How do I find out the screen orientation, in portrait or landscape?
-
19-06-2021 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow