This question arose when I was making programmatically-created view controller. This controller was supposed to support any interface orientation. And according to good practice I was creating subviews on -viewDidLoad
and setting up their frames on -viewWillLayoutSubviews
. Such approach is good, because -viewWillLayoutSubviews
not only layout your subviews on the beginning but also handles interface rotations and other changes of view controller's view.
On iOS 8 everything was ok, but when i was running the app on iOS 6 and 7 layout was broken. I found out that the reason was that the layout was based on self.view.frame
property. And if you try to print the frame on -viewWillLayoutSubviews
on different iOS versions, you will get different results, when device is in landscape mode.
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
NSLog(@"%@", NSStringFromCGRect(self.view.frame));
}
Output on iOS 8 (after turning device to landscape mode):
2014-10-07 17:35:11.028 BlaBlaApp[361:12587] {{0, 0}, {568, 320}}
Output on iOS 7 and 6 (same device orientation):
2014-10-07 17:35:11.028 BlaBlaApp[361:12587] {{0, 0}, {320, 568}}
On iOS 7 and 6 view frame doesn't change when device orientation changes. But it was obviously for me that visible area of the view changes! I decided to investigate how actually interface rotation works...
On iOS 7 and 6 view is rotated by changing its transform
property ( frame
is unchanged). On iOS 8, in turn, transform
property of the view controller's view is always equal to CGAffineTransformIdentity
and the frame
changes depending on orientation.
I managed to solve my problem with one string of code (without macros and if statements):
CGRect actualFrame = CGRectApplyAffineTransform(self.view.frame, self.view.transform);
Subviews can be positioned according to actualFrame
in the similar way on any iOS version.
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.