Krunal
Krunal

Reputation: 6490

Unable to see UIActivityIndicator in UIWebView

I am new to iPhone developer,

I am loading pages in my webview, loading takes too much time so i wan to show ActivityIndicator till the page loads,

Here is my code snippet but it is not working,

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;
}

but i am unable to see my activityIndicator

Any help will be appreciated.

Upvotes: 1

Views: 985

Answers (3)

tipycalFlow
tipycalFlow

Reputation: 7644

You can also change the order in which you add subviews. Add the webview first, and then add the 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];

And get rid of the line that follows:[_webview bringSubviewToFront:activityIndicator];

Upvotes: 1

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

You are adding the UIActivityIndicatorView directly to the self.view and later you are trying to bringSubviewToFront from the UIWebView. Check with:

[self.view bringSubviewToFront:activityIndicator];

Upvotes: 3

Praveen S
Praveen S

Reputation: 10393

Change [_webview bringSubviewToFront:activityIndicator]; to [self.view bringSubviewToFront:activityIndicator];

Upvotes: 1

Related Questions