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

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

    I needed a quick helper function that kept the same behavior as iOS7 under iOS8 - this allowed me to swap out my [[UIScreen mainScreen] bounds] calls and not touch other code...

    + (CGRect)iOS7StyleScreenBounds {
        CGRect bounds = [UIScreen mainScreen].bounds;
        if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
            bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
        }
            return bounds;
    }
    
    0 讨论(0)
  • 2020-11-22 17:44

    My issue was related to UIWindows frame which was going in minus. So made the code as below in MyViewController -(NSUInteger)supportedInterfaceOrientations Method

    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    [self.view setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    
    [appDel.window setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    

    And its work for me try it.

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

    Related to this question as it solved my problem, here two defines I use for screen width and height calculations:

    #define SCREEN_WIDTH (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height) : [[UIScreen mainScreen] bounds].size.width)
    
    #define SCREEN_HEIGHT (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width) : [[UIScreen mainScreen] bounds].size.height)
    
    #define IOS_VERSION_LOWER_THAN_8 (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
    

    If you are supporting both iOS 7 and iOS 8, this is the best solution for this problem.

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

    One thing that I noted is, the order of supported interface orientations in Info.plist does matter. I got the problem of this question with my app (that does orientation in code), but I did not specify anywhere that default orientation is Portrait.

    I thought that default orientation was Portrait in any case.

    Rearranging itens in Info.plist, putting Portrait first, restored the expected behavior.

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

    Yes, it's now dependent on orientation.

    I prefer the below method of getting the screen size in an orientation-independent way to some of the answers above, both because it's simpler and because it doesn't depend on any of the orientation code (the state of which can be dependent on the time that they are called) or on version checking. You may want the new iOS 8 behavior, but this will work if you need it to be stable on all versions of iOS.

    +(CGSize)screenSizeOrientationIndependent {
         CGSize screenSize = [UIScreen mainScreen].bounds.size;
         return CGSizeMake(MIN(screenSize.width, screenSize.height), MAX(screenSize.width, screenSize.height));
    }
    
    0 讨论(0)
  • 2020-11-22 17:49

    You can use nativeBounds (orientation-independent)

    nativeBounds

    The bounding rectangle of the physical screen, measured in pixels. (read-only)

    Declaration SWIFT

      var nativeBounds: CGRect { get }
    

    This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.

    Detecting the device's height:

    if UIScreen.mainScreen().nativeBounds.height == 960.0 {
    
    }
    

    Detecting the device's width:

    if UIScreen.mainScreen().nativeBounds.width == 640.0 {
    
    }
    
    0 讨论(0)
提交回复
热议问题