Search code examples
iosobjective-ccocoa-touchviewdidlayoutsubviews

How do I get the correct view height in viewDidLayoutSubviews?


- (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?


Solution

  • 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.