Why is my UIActivityIndicatorView not centered on the screen?

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

It is currently displayed in the lower half.

+3
source share
6 answers

This will put the indicator in the center of your view (and not on 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)

Auto-resistance is maintained in the center when its size changes (for example, when switching the screen orientation).

If this does not work, your problem is somewhere else. Try to write down the size of your view, is it possible that it is located incorrectly?

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

You can do it as follows:

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
+2
source

activityIndicator.center =  self.view.center;

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

, ,

+2

If you add an indicator to parentView, the following logic should center the indicator on the parent - this works for me.

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

Set center to viewDidLayoutSubviews.

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

Thank:)

0
source

I also have this problem, try this

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

All Articles