【问题标题】:Add an activity indicator on a uiwebview在 uiwebview 上添加活动指示器
【发布时间】:2013-10-28 19:31:33
【问题描述】:

我有一个 viewDidLoad,它创建一个 web 视图并启动一个活动指示器。如何使活动指示器仅在网页出现时停止?如何在活动指示器中添加诸如“正在加载”之类的词?

// Create the UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

[self.view addSubview:webView];

// Start the throbber to check if the user exists
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = CGPointMake([self screenWidth] / 2.0, 370.0);
[activityView startAnimating];
activityView.tag = 100;
[self.view addSubview:activityView];

NSString* url = @"http://google.com";
NSURL* nsUrl = [NSURL URLWithString:url];
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];


// Remove activity view
UIView *viewToRemove = [self.view viewWithTag:100];
//[viewToRemove removeFromSuperview];

[webView loadRequest:request];

【问题讨论】:

  • 在下面查看我的答案

标签: ios uiwebview ios7 uiactivityindicatorview


【解决方案1】:

如何使活动指示器仅在网页出现时停止?

你的对象可以变成delegate,让webView监听-webViewDidFinishLoad委托方法。所以:

- (void)viewDidLoad {
    // Create the UIWebView
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self; // Here is the key
    ...
}

...

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.view viewWithTag:100].hidden = YES;
}

您也可以实现-webViewDidStartLoad: 来显示活动指示器,而不是在-viewDidLoad 方法中显示它。

如何在活动指示器中添加单词,例如“正在加载”?

你应该创建一个单独的UILabel,没有办法在标准UIActivityIndicatorView中添加文本

【讨论】:

  • 这给了我一个警告,因为隐私控制器是一个视图控制器:webView.delegate = self; // 这里是关键
  • 如何解决这个问题以消除警告?
  • 哎呀忘了把这个添加到头文件
【解决方案2】:

制作一个加载视图:

@implementation yourViewController{;
    UIView* loadingView;
}

在你的viewDidLoad:

loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
loadingView.layer.cornerRadius = 5;

UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
[activityView startAnimating];
activityView.tag = 100;
[loadingView addSubview:activityView];

UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
lblLoading.text = @"Loading...";
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
lblLoading.textAlignment = NSTextAlignmentCenter;
[loadingView addSubview:lblLoading];

[self.view addSubview:loadingView];

此视图将如下所示:

注意,如果你想使用cornerRadius,你必须导入<QuartzCore/QuartzCore.h> 框架,并且在导入之前,将QuartzCore 框架添加到你的项目中!

检测 webview 何时停止加载:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [loadingView setHidden:YES];
}

你必须实现UIWebViewDelegate协议,并且

webView.delegate = self;

并使其可见:

- (void)webViewDidStartLoad:(UIWebView *)webView {

 [loadingView setHidden:NO];

}

【讨论】:

    【解决方案3】:

    如果你在 Swift 上寻找这个,这里是:

    var boxView = UIView()
    
    func webViewDidStartLoad(webView_Pages: UIWebView) {
    
        // Box config:
        boxView = UIView(frame: CGRect(x: 115, y: 110, width: 80, height: 80))
        boxView.backgroundColor = UIColor.blackColor()
        boxView.alpha = 0.9
        boxView.layer.cornerRadius = 10
    
        // Spin config:
        let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        activityView.frame = CGRect(x: 20, y: 12, width: 40, height: 40)
        activityView.startAnimating()
    
        // Text config:
        let textLabel = UILabel(frame: CGRect(x: 0, y: 50, width: 80, height: 30))
        textLabel.textColor = UIColor.whiteColor()
        textLabel.textAlignment = .Center
        textLabel.font = UIFont(name: textLabel.font.fontName, size: 13)
        textLabel.text = "Loading..."
    
        // Activate:
        boxView.addSubview(activityView)
        boxView.addSubview(textLabel)
        view.addSubview(boxView)
    }
    
    func webViewDidFinishLoad(webView_Pages: UIWebView) {
    
        // Removes it:
        boxView.removeFromSuperview()
    
    }
    

    别忘了调用 UIWebViewDelegate:

    class ViewController: UIViewController, UIWebViewDelegate {
    

    并在 viewDidLoad 上设置 webView 的委托:

    webView.delegate = self
    

    【讨论】:

      【解决方案4】:

      标题:

          UIView * indicatorView;
          UIActivityIndicatorView *activityIndicator;
      

      ViewDidLoad:

      @property UILabel* label;
      
      
      -(void)viewDidLoad {
      [super viewDidLoad];
      
      
      indicatorView=[[UIView alloc]initWithFrame:CGRectMake(self.mapView.frame.size.width/2-40, self.mapView.frame.size.height/2-30 , 80, 60)];
      indicatorView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.7f];
      [self.mapView addSubview:indicatorView];
      indicatorView.layer.cornerRadius=5;
      [[indicatorView layer] setBorderWidth:1.0f];
      [[indicatorView layer] setBorderColor:[UIColor whiteColor].CGColor];
      indicatorView.clipsToBounds=YES;
      
      
      activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(indicatorView.frame.size.width/2-15, 8, 30, 30)];
      [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
      //activityIndicator.backgroundColor=[UIColor greenColor];
      [indicatorView addSubview:activityIndicator];
      [activityIndicator startAnimating];
      
      
      UILabel *loadingLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, indicatorView.frame.size.height-22, indicatorView.frame.size.width, 20)];
      loadingLbl.text = @"  Loading...";
      //loadingLbl.backgroundColor=[UIColor redColor];
      loadingLbl.textColor = [UIColor whiteColor];
      loadingLbl.textAlignment = NSTextAlignmentCenter;
      loadingLbl.font = [UIFont fontWithName:@"YanaR-Bold" size:10];
      [loadingLbl  setFont:[UIFont boldSystemFontOfSize:10.0f]];
      [indicatorView addSubview:loadingLbl];
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      相关资源
      最近更新 更多