1

我是 iPhone 开发者的新手,

我正在我的 webview 中加载页面,加载需要太多时间,所以我想显示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

任何帮助将不胜感激。

4

3 回答 3

3

您正在将UIActivityIndicatorView直接添加到 ,self.view然后您尝试bringSubviewToFrontUIWebView. 检查:

[self.view bringSubviewToFront:activityIndicator];
于 2012-07-05T06:44:22.230 回答
1

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

于 2012-07-05T06:43:21.030 回答
1

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

_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-07-05T06:46:21.560 回答