简体   繁体   中英

iOS center a view

I have an class which adds a UIActivityIndicator to a specified view. I want to center the UIActivityIndicator in the given view.

I thought this work correctly since the UIActivityIndicator wasn't on screen very long. Now I see that when I rotate the screen when he is active, the UIActivityIndicator isn't centered anymore.

The way I did it was:

CGRect frame = CGRectMake((view.frame.size.width / 2 - self.activityIndicator.frame.size.width / 2),
                          (view.frame.size.height / 2 - self.activityIndicator.frame.size.height / 2),
                          self.activityIndicator.frame.size.width,
                          self.activityIndicator.frame.size.height
                         );

Which gives a better result then:

self.activityIndicator.center = view.center;

Is there some way I can add a constraint so it stays in the middle even when rotated. I know this can be done in IB, but in code I have no idea?

您的代码,加上:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM