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

后端 未结 18 999
北恋
北恋 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: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]; 
    

提交回复
热议问题