无法在UIWebView中看到UIActivityIndi​​cator

时间:2012-07-05 06:37:13

标签: iphone ipad uiwebview loading uiactivityindicatorview

我是iPhone开发人员的新手,

我正在我的网页浏览中加载页面,加载需要花费太多时间,所以我要显示ActivityIndicator直到页面加载,

这是我的代码段,但它不起作用,

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

我们将不胜感激。

3 个答案:

答案 0 :(得分:3)

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

[self.view bringSubviewToFront:activityIndicator];

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您还可以更改添加子视图的顺序。首先添加webview,然后添加活动指示符:

_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];