【问题标题】:Unable to see UIActivityIndicator in UIWebView在 UIWebView 中看不到 UIActivityIndi​​cator
【发布时间】:2012-07-05 06:37:13
【问题描述】:

我是 iPhone 开发者的新手,

我正在我的 webview 中加载页面,加载需要太多时间,所以我想在页面加载之前显示 ActivityIndicator

这是我的代码 sn-p 但它不起作用,

activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
    activityIndicator.center = self.view.center;
    [self.view addSubview: activityIndicator];

    _webview=[[UIWebView alloc]init];
    [_webview setBackgroundColor:[UIColor grayColor]];
    [_webview setDelegate:(id<UIWebViewDelegate>)self];
    [self.view addSubview:_webview];

    [_webview bringSubviewToFront:activityIndicator];

...

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

     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

     [activityIndicator startAnimating];
     activityIndicator.hidden=FALSE;
 }

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

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    [activityIndicator stopAnimating];
    activityIndicator.hidden=TRUE;
}

但我看不到我的activityIndicator

我们将不胜感激。

【问题讨论】:

    标签: iphone ipad uiwebview loading uiactivityindicatorview


    【解决方案1】:

    您正在将UIActivityIndicatorView 直接添加到self.view,然后您尝试从UIWebView 中添加bringSubviewToFront。检查:

    [self.view bringSubviewToFront:activityIndicator];
    

    【讨论】:

      【解决方案2】:

      [_webview bringSubviewToFront:activityIndicator]; 更改为[self.view bringSubviewToFront:activityIndicator];

      【讨论】:

        【解决方案3】:

        您还可以更改添加子视图的顺序。先添加webview,再添加activity指示器:

        _webview=[[UIWebView alloc]init];
        [_webview setBackgroundColor:[UIColor grayColor]];
        [_webview setDelegate:(id<UIWebViewDelegate>)self];
        [self.view addSubview:_webview];
        
        activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);//you should also keep the height and width equal
        activityIndicator.center = self.view.center;
        [self.view addSubview: activityIndicator];
        

        并去掉后面的行:[_webview bringSubviewToFront:activityIndicator];

        【讨论】:

          猜你喜欢
          • 2012-06-21
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-20
          相关资源
          最近更新 更多