Besfort Abazi
Besfort Abazi

Reputation: 383

UIViewController's view.center not always the same value

My animations in a view controller depend on there being a fixed value of self.view.center. However, depending on my navigation flow, my self.view.center has a different value (41.5f difference on iPhone X, iOS 11).

Can someone explain why that would be? Has it something to do with extended layouts? Changing the status bar/tab bar/nav bar?

Can I get the exact value through another method?

I have already tried:

CGRectGetMidY(self.view.bounds); //-> same problem applies here

Upvotes: 0

Views: 49

Answers (2)

Sivaguru
Sivaguru

Reputation: 1

If you are accessing center or bounds in the viewDidLoad () method. Try it in the viewDidAppear() it will give you the correct center position of the view controller's view

Upvotes: 0

jscs
jscs

Reputation: 64002

UIView's center property is the center of the view's frame. The frame's coordinate system is that of the superview. So, indeed, the value of center depends on the context where the view finds itself.

For the center of the view in its own coordinate system, you need to look at the its bounds rectangle. The CGRectGetMidX() and CGRectGetMidY() will give you the values you need for the center point.

Upvotes: 1

Related Questions