iOS7、iOS8 UIScreen方法差异

iOS7、iOS8 UIScreen方法差异

iOS8 在UI的Storyboard设计上引入Size Classes观念后,对于画面的横向、直向的解释已经不同,UIScreen上面的bonds、applicationFrame所回报的值也因为这样而有所不同,接下来在iOS7、iOS8分别执行下面的程式并观察结果看看有什么不同?

//-----------start-----------
NSLog(@"bounds: width = %f, height = %f", [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
NSLog(@"applicationFrame: width = %f, height = %f",[UIScreen mainScreen].applicationFrame.size.width,[UIScreen mainScreen].applicationFrame.size.height);
//------------end------------

iOS7

  • 直向
bounds:           width = 768.000000, height = 1024.000000
applicationFrame: width = 768.000000, height = 1004.000000

  • 横向
bounds:           width = 768.000000, height = 1024.000000
applicationFrame: width = 748.000000, height = 1024.000000

iOS8

  • 直向
bounds:           width = 768.000000, height = 1024.000000
applicationFrame: width = 768.000000, height = 1004.000000

  • 横向
bounds:           width = 1024.000000, height = 768.000000
applicationFrame: width = 1024.000000, height = 748.000000

没错,现在的width、height是以你实际操作角度来看width、height,所以在iOS8中你将iPad横向之后,width从原先直向的768变成1024,其实iOS8的做法个人觉的与实际操作想法比较OK。

横向、直向 判别

iOS7

iOS7 中提供一个非常容易使用的方法,如果你要知道现在iPad是直向还是横向直接像下面程式一样:

//-----------start-----------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration {
    // 旋转之前
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // 旋转之后
    // 由 fromInterfaceOrientation 值得知目前是哪个方向
    /*
    UIInterfaceOrientationPortrait
    UIInterfaceOrientationPortraitUpsideDown
    UIInterfaceOrientationLandscapeLeft
    UIInterfaceOrientationLandscapeRight
    */
}
//------------end------------

iOS8

iOS8 后需要用另一种方式,这方式是使用width来判断:

//-----------start-----------
- (void)viewWillTransitionToSize:(CGSize)size 
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    if (size.width <= size.height) {
        // 画面直向
    } else {
        // 画面横向
    }
}
//------------end------------