简体   繁体   中英

How do I find out the screen orientation, in portrait or landscape?

I can't believe this isn't possible. It's just one of those things that surely everyone must require.

So, this:

[UIScreen currentScreen] bounds];

returns a width of 320 on my iPhone, regardless of whether the phone is in portrait or landscape. So I made this helper method:

+(CGRect)screenFrame {

    CGRect frame = [UIScreen mainScreen].bounds;

    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
        return frame;
    }
    else {
        CGRect newFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width);
        return newFrame;
    }

}

Which works great, except when the device is laying flat, apparently that makes it think it's in landscape, even if the screen doesn't change to landscape.

How can i tweak this to get it working?

The best guidance (largely because of the 'laying flat' orientation) is to use [[UIApplication sharedApplication] statusBarOrientation] . This will then give you the correct value based on how the application has adjusted the main window's coordinate transform matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM