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

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

    My solution is a combination of MaxK's and hfossli. I made this method on a Category of UIScreen and it has no version checks (which is a bad practice):

    //Always return the iOS8 way - i.e. height is the real orientation dependent height
    + (CGRect)screenBoundsOrientationDependent {
        UIScreen *screen = [UIScreen mainScreen];
        CGRect screenRect;
        if (![screen respondsToSelector:@selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
            screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
        } else {
            screenRect = screen.bounds;
        }
    
        return screenRect;
    }
    
    0 讨论(0)
  • 2020-11-22 17:25

    Used slightly modified mnemia's solution, that one without iOS version check, using min/max on mainscreen bounds.
    I needed a CGRect so got CGRect from mainscreen bounds and changed size.width=min(w,h), size.height=max(w,h). Then I called that OS-independent get CGRect function in two places in my code, where I getting screen size for OpenGL, touches etc. Before fix I had 2 problems - on IOS 8.x in landscape mode display position of OpenGL view was incorrect: 1/4 of full screen in left bottom part. And second touches returned invalid values. Both problems were fixed as explained. Thanks!

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

    Just adding the swift version of an excellent cbartel function answered above.

    func screenSize() -> CGSize {
        let screenSize = UIScreen.mainScreen().bounds.size
        if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
            return CGSizeMake(screenSize.height, screenSize.width)
        }
        return screenSize
    }
    
    0 讨论(0)
  • 2020-11-22 17:33

    Yes, it's orientation-dependent in iOS8, not a bug. You could review session 214 from WWDC 2014 for more info: "View Controller Advancements in iOS 8"

    Quote from the presentation:

    UIScreen is now interface oriented:

    • [UIScreen bounds] now interface-oriented
    • [UIScreen applicationFrame] now interface-oriented
    • Status bar frame notifications are interface-oriented
    • Keyboard frame notifications are interface-oriented
    0 讨论(0)
  • 2020-11-22 17:33

    Yes, indeed, screen size is now orientation dependent in iOS 8. Sometimes, however, it's desired to get a size fixed to portrait orientation. Here is how I do it.

    + (CGRect)screenBoundsFixedToPortraitOrientation {
        UIScreen *screen = [UIScreen mainScreen];
    
        if ([screen respondsToSelector:@selector(fixedCoordinateSpace)]) {
                        return [screen.coordinateSpace convertRect:screen.bounds toCoordinateSpace:screen.fixedCoordinateSpace];
        } 
        return screen.bounds;
    }
    
    0 讨论(0)
  • 2020-11-22 17:37

    Yes, it's orientation-dependent in iOS8.

    I wrote a Util method to resolve this issue for apps that need to support older versions of the OS.

    + (CGSize)screenSize {
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
            return CGSizeMake(screenSize.height, screenSize.width);
        }
        return screenSize;
    }
    
    0 讨论(0)
提交回复
热议问题