Is [UIScreen mainScreen].bounds.size becoming orientation-dependent in iOS8?

后端 未结 18 1051
北恋
北恋 2020-11-22 16:55

I ran the following code in both iOS 7 and iOS 8:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
BOOL landsca         


        
18条回答
  •  长情又很酷
    2020-11-22 17:39

    Yes, it's orientation-dependent in iOS8.

    Here's how you can have a concistent way of reading out bounds in an iOS 8 fashion across SDK's and OS-versions.

    #ifndef NSFoundationVersionNumber_iOS_7_1
    # define NSFoundationVersionNumber_iOS_7_1 1047.25
    #endif
    
    @implementation UIScreen (Legacy)
    
    // iOS 8 way of returning bounds for all SDK's and OS-versions
    - (CGRect)boundsRotatedWithStatusBar
    {
        static BOOL isNotRotatedBySystem;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            BOOL OSIsBelowIOS8 = [[[UIDevice currentDevice] systemVersion] floatValue] < 8.0;
            BOOL SDKIsBelowIOS8 = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1;
            isNotRotatedBySystem = OSIsBelowIOS8 || SDKIsBelowIOS8;
        });
    
        BOOL needsToRotate = isNotRotatedBySystem && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
        if(needsToRotate)
        {
            CGRect screenBounds = [self bounds];
            CGRect bounds = screenBounds;
            bounds.size.width = screenBounds.size.height;
            bounds.size.height = screenBounds.size.width;
            return bounds;
        }
        else
        {
            return [self bounds];
        }
    }
    
    @end
    

提交回复
热议问题