How do I get the correct view height in viewDidLayoutSubviews?

北慕城南 提交于 2019-12-24 03:09:14

问题


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"viewDidLayoutSubviews view.height: %f", self.view.frame.size.height);
}

In the above, view.height is always 1024 regardless of what orientation the device is in. Even rotating the device will cause viewDidLayoutSubviews to be called, and it will still output 1024.

Yet the view renders fine. What the heck am I missing?


回答1:


When iOS performs an automatic rotation, it does not change the frame property of the root view controller's view. What it does is apply a transform and animate the transform change. To see the "actual" size of your view, check the bounds property instead of the frame property.

To quote Apple's documentation of the transform property:

Warning: If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.




回答2:


I think it's time to update in iOS8. In iOS8:

  • UIScreen is now interface oriented:
  • [UIScreen bounds] now interface-oriented
  • [UIScreen applicationFrame] now interface-oriented
  • Status bar frame notifications are interface-oriented
  • Keyboard frame notifications are interface-oriented


来源:https://stackoverflow.com/questions/19505399/how-do-i-get-the-correct-view-height-in-viewdidlayoutsubviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!