Question

This is driving me crazy. I have a webview and a UIActivityIndicator.

For some reason it will not load in the center of the webview and is instead loading off to the left.

Tried several tutorials to get this working but cannot figure it out.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    NSLog(@"PASSED URL IS %@", passedURL);
    NSString *fullURL = passedURL;
    //NSString *fullURL = @"http://107.22.183.71/index.php";
    NSURL *url = [NSURL URLWithString:fullURL];

    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    //Put the indicator on the center of the webview
    [activityIndicator setCenter:self.view.center];

    //Assign it to the property
    self.activityIndicator=activityIndicator;

    //Add the indicator to the webView to make it visible
    [self.webView addSubview:self.activityIndicator];


    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requestObj]; 
}

I suspect its my order of loading the webview or the indicator.

The indicator itself works fine and I have implemented all the require delegates.

Any help greatly appreciated.

Was it helpful?

Solution

The geometry of your view is not set at this point (viewDidLoad). Place your UIActivityIndicator in viewWillAppear: and you should be fine.

OTHER TIPS

Try [self.view addSubview:self.activityIndicator]; instead of [self.webView addSubview:self.activityIndicator];

My guess is your webView is not taking entire screen, so you need to add it to self.view

Maybe views self.view and self.webview don't have the same center.
Try to replace

[activityIndicator setCenter:self.view.center];

by

[activityIndicator setCenter:self.webView.center];

Several things:

Are you using auto-layout? If so, you will need to add a constraint that pins your activity indicator's center to the web view's center.

Next, where is your web view in your view hierarchy?

Since you're adding your activity indicator as a subview of the web view, it's center position should be expressed in the web view's coordinate system, not in the view controller's main view's coordinate system.

Try this:

CGPoint center = CGPointMake(CGRectGetMidX(_webView.bounds), CGRectGetMid(_webView.bounds)); 
[activityIndicator setCenter: center];

I don't know if you're going to have problems with the web view putting things on top of your activity indicator.

Rather than adding your activity indicator as a subview of your web view, you might want to add it to the view controller's view, placed on top of the web view and positioned to center it there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top