Strong Like Bull
Strong Like Bull

Reputation: 11317

Why is my UIActivityIndicatorView is not centered on screen?

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);

Presently it appears in the bottom half.

Upvotes: 3

Views: 5863

Answers (6)

Mohsin Qureshi
Mohsin Qureshi

Reputation: 1213

Set center in viewDidLayoutSubviews.

- (void) viewDidLayoutSubviews {
self.myActivityIndicator.center = self.view.center; }

Thanks :)

Upvotes: 0

Edwin O.
Edwin O.

Reputation: 5276

why not just this way

activityIndicator.center =  self.view.center;

activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)

this is working well without having to do those calculations as mentioned by others

Upvotes: 2

Ramesh
Ramesh

Reputation: 1763

If you are adding indicator to parentView following logic should center the indicator on parent - It works for me.

 // ...
 CGSize parentSize = parentView.frame.size;
 UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 activityIndicator.center = CGPointMake(parentSize.width/2, parentSize.height/2);
 // ...

Upvotes: 1

Sulthan
Sulthan

Reputation: 130200

This will put the indicator in the center of your view (not the screen!):

activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)

The autoresizing keeps in in the center when its size changes (e.g. when switching screen orientation).

If it doesn't work, your problem is somewhere else. Try to log the size of your view, maybe it is not positioned correctly?

NSLog(@"View frame: %@", NSStringFromCGRect(self.view.frame));

Upvotes: 15

Desmond
Desmond

Reputation: 5011

i also have this problem, try this

activityIndicator.center =CGPointMake(480/2.0, 320/2.0);

Upvotes: -3

johnoodles
johnoodles

Reputation: 349

You can do it this way:

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(self.view.bounds.size.width / 2.0f - activityIndicator.frame.size.width /2.0f, self.view.bounds.size.height / 2.0f - activityIndicator.frame.size.height /2.0f, activityIndicator.frame.size.width, activityIndicator.frame.size.height);
//then add to the view

Upvotes: 2

Related Questions