原文 http://www.cnblogs.com/smileEvday/p/iOS8.html
1、UIWindow的bounds
iOS 7之前Window的bounds不会随着方向而变化,但是到了iOS 8以后,随着设备方向的旋转,window.bounds.size.width和window.bounds.size.height也会相应发生变化。
做个很简单的测试,代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil]; return YES;
} - (void)orientationChanged:(NSNotification*)noti { UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *orientationDes = nil;
switch (orientation) {
case UIDeviceOrientationLandscapeLeft:
orientationDes = @"UIInterfaceOrientationLandscapeRight";
break;
case UIDeviceOrientationLandscapeRight:
orientationDes = @"UIInterfaceOrientationLandscapeLeft";
break;
case UIDeviceOrientationPortrait:
orientationDes = @"UIInterfaceOrientationPortrait";
break;
case UIDeviceOrientationPortraitUpsideDown:
orientationDes = @"UIInterfaceOrientationPortraitUpsideDown";
break;
default:
orientationDes = @"";
break;
} NSLog(@"system ver: %@, \rorientaion: %@, \rwindow bounds: %@",
[UIDevice currentDevice].systemVersion,
orientationDes,
NSStringFromCGRect(self.window.bounds));
}
示例代码很简单,新建一个工程,然后在delegate中直接添加以上代码即可。
iOS 8上运行结果为:
-- ::32.016 SviOS8[:] system ver: 8.0,
orientaion: UIInterfaceOrientationLandscapeRight,
window bounds: {{, }, {, }}
-- ::34.788 SviOS8[:] system ver: 8.0,
orientaion: UIInterfaceOrientationPortrait,
window bounds: {{, }, {, }}
-- ::35.791 SviOS8[:] system ver: 8.0,
orientaion: UIInterfaceOrientationLandscapeLeft,
window bounds: {{, }, {, }}
-- ::47.468 SviOS8[:] system ver: 8.0,
orientaion: UIInterfaceOrientationPortraitUpsideDown,
window bounds: {{, }, {, }}
iOS 7及之前的版本运行结果为:
-- ::00.527 SviOS8[:70b] system ver: 7.0.,
orientaion: UIInterfaceOrientationLandscapeRight,
window bounds: {{, }, {, }}
-- ::00.895 SviOS8[:70b] system ver: 7.0.,
orientaion: UIInterfaceOrientationPortrait,
window bounds: {{, }, {, }}
-- ::01.225 SviOS8[:70b] system ver: 7.0.,
orientaion: UIInterfaceOrientationLandscapeLeft,
window bounds: {{, }, {, }}
-- ::11.004 SviOS8[:70b] system ver: 7.0.,
orientaion: UIInterfaceOrientationPortraitUpsideDown,
window bounds: {{, }, {, }}
通过对比我们可以清晰的看到iOS 8中UIWindow在处理旋转时策略的变更,虽然会因为与之前的版本不同导致现有项目布局存在的bug,但是可以看到iOS 8中的处理方式更加符合我们的预期,在竖向的时候我们就获取到width < height, 在横向则是 width > height,这个符合所见即所得的原则。