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

后端 未结 18 883
北恋
北恋 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:37

    iOS 8 or upper

    A solution for those who want to find out the screen size in points (3.5 inches screen has 320 × 480 points, 4.0 inches screen has 320 × 568 points, etc) would be

    - (CGSize)screenSizeInPoints
    {
        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    
        if (width > height) {
            return CGSizeMake(height, width);
        }
        else {
            return [[UIScreen mainScreen] bounds].size;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:38

    It is not a bug in iOS 8 SDK. They made bounds interface orientation depended. According to your question about some reference or documentation to that fact I will highly recommend you to watch View Controller Advancements in iOS 8 it is 214 session from WWDC 2014. The most interesting part (according to your doubts) is Screen Coordinates which starts at 50:45.

    0 讨论(0)
  • 2020-11-22 17:38

    This will give correct device in iOS7 and iOS8 both,

    #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define IS_PORTRAIT         UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)
    
    + (BOOL)isIPHONE4{
    
    // < iOS 8.0
    if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
    
            if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 480.0) {
                return YES;
            } else {
                return NO;
            }
    
    // >= iOS 8.0
    }else{
    
        if(IS_PORTRAIT){
    
            if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 480.0) {
                return YES;
            } else {
                return NO;
            }
    
        }else{
    
            if ([self getDeviceWidth] == 480.0 && [self getDeviceHeight] == 320.0) {
                return YES;
            } else {
                return NO;
            }
    
        }
    
    }
    
    
    }
    
    + (BOOL)isIPHONE5{
    
    
    // < iOS 8.0
    if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
    
        if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 568.0) {
            return YES;
        } else {
            return NO;
        }
    
        // >= iOS 8.0
    }else{
    
        if(IS_PORTRAIT){
    
            if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 568.0) {
                return YES;
            } else {
                return NO;
            }
    
        }else{
    
            if ([self getDeviceWidth] == 568.0 && [self getDeviceHeight] == 320.0) {
                return YES;
            } else {
                return NO;
            }
    
        }
    
    }
    
    }
    
    + (BOOL)isIPHONE6{
    
    // < iOS 8.0
    if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
    
        if ([self getDeviceWidth] == 375.0 && [self getDeviceHeight] == 667.0) {
            return YES;
        } else {
            return NO;
        }
    
        // >= iOS 8.0
    }else{
    
        if(IS_PORTRAIT){
    
            if ([self getDeviceWidth] == 375.0 && [self getDeviceHeight] == 667.0) {
                return YES;
            } else {
                return NO;
            }
    
        }else{
    
            if ([self getDeviceWidth] == 667.0 && [self getDeviceHeight] == 375.0) {
                return YES;
            } else {
                return NO;
            }
    
        }
    
    }
    
    
    }
    + (BOOL)isIPHONE6Plus{
    
    
    // < iOS 8.0
    if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
    
        if ([self getDeviceWidth] == 414.0 && [self getDeviceHeight] == 736.0) {
            return YES;
        } else {
            return NO;
        }
    
        // >= iOS 8.0
    }else{
    
        if(IS_PORTRAIT){
    
            if ([self getDeviceWidth] == 414.0 && [self getDeviceHeight] == 736.0) {
                return YES;
            } else {
                return NO;
            }
    
        }else{
    
            if ([self getDeviceWidth] == 736.0 && [self getDeviceHeight] == 414.0) {
                return YES;
            } else {
                return NO;
            }
    
        }
    
    }
    
    
    }
    
    + (CGFloat)getDeviceHeight{
    
    //NSLog(@"Device width: %f",[UIScreen mainScreen].bounds.size.height);
    return [UIScreen mainScreen].bounds.size.height;
    }
    + (CGFloat)getDeviceWidth{
    
    //NSLog(@"Device width: %f",[UIScreen mainScreen].bounds.size.height);
    return [UIScreen mainScreen].bounds.size.width;
    }
    

    //You may add more devices as well(i.e.iPad).

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 17:41

    The below method can be used to find the screen bounds for a given orientation, independent of iOS version. This method will return the bounds based on the screen size of the device and will give the same CGRect value independent of iOS version.

    - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
    
        CGFloat width   = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height  = [[UIScreen mainScreen] bounds].size.height;
    
        CGRect bounds = CGRectZero;
    
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            bounds.size = CGSizeMake(MAX(width, height), MIN(width, height));
        } else {
            bounds.size = CGSizeMake(MIN(width, height), MAX(width, height));
        }
    
        return bounds;
    }
    
    // For the below example, bounds will have the same value if you run the code on iOS 8.x or below versions.
    CGRect bounds = [self boundsForOrientation:UIInterfaceOrientationPortrait]; 
    
    0 讨论(0)
  • 2020-11-22 17:41

    Thats what I used to calculate the correct rect:

    UIScreen* const mainScreen = [UIScreen mainScreen];
    CGRect rect = [mainScreen bounds];
    #ifdef __IPHONE_8_0
    if ([mainScreen respondsToSelector:@selector(coordinateSpace)])
    {
        if ([mainScreen respondsToSelector:@selector(fixedCoordinateSpace)])
        {
            id tmpCoordSpace = [mainScreen coordinateSpace];
            id tmpFixedCoordSpace = [mainScreen fixedCoordinateSpace];
    
            if ([tmpCoordSpace respondsToSelector:@selector(convertRect:toCoordinateSpace:)])
            {
                rect = [tmpCoordSpace convertRect:rect toCoordinateSpace: tmpFixedCoordSpace];
            }
        }
    }
    #endif
    
    0 讨论(0)
提交回复
热议问题