开发者

Why is my UIActivityIndicatorView is not centered on screen?

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewSty开发者_JAVA百科leWhiteLarge];
        activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);

Presently it appears in the bottom half.


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


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


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


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);
 // ...


Set center in viewDidLayoutSubviews.

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

Thanks :)


i also have this problem, try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜