0

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
jscs
  • 63,694
  • 13
  • 151
  • 195
Besfort Abazi
  • 373
  • 3
  • 18

2 Answers2

1

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • You mean CGRectGetMidY(self.view.bounds);? I have already tried that, but there is still a different value (by 41.5 points). – Besfort Abazi Jan 11 '19 at 18:38
  • Is the view's size different? Is there a transform applied? – jscs Jan 11 '19 at 18:46
  • No, not at all. It is the same view in a ViewController, all it depends on is the navigation path before it gets called (with one particular ViewController that seems to cause the view.center-change, but I have no idea what changes in that ViewController). – Besfort Abazi Jan 11 '19 at 18:56
0

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

Sivaguru
  • 1
  • 1
  • I have tried to get the values in both the viewDidAppear() and viewDidLoad() methods. Also tried: viewWillAppear() viewDidLayoutSubviews() -> same problem there. – Besfort Abazi Jan 15 '19 at 11:51