Determine UIInterfaceOrientation on iPad
Solution 1
Are you aware of the interfaceOrientation
property of the UIViewController
class?
- (void) viewDidLoad {
[super viewDidLoad];
BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
// now do whatever you need
}
Or are you after [[UIDevice currentDevice] orientation]
?
Solution 2
Especially at launch I have found the following to be always accurate for the UI, regardless of what the UIDevice says the orientation is.
[UIApplication sharedApplication].statusBarOrientation
Solution 3
self.interfaceOrientation
is unreliable in certain situations. For example, re-arranging tabs in a tabbar application returns incorrect value.
However [UIApplication sharedApplication].statusBarOrientation
is always reliable. You saved me a lot of time slycrel. Thank you.
Solution 4
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ((orientation == UIInterfaceOrientationLandscapeLeft)
|| (orientation == UIInterfaceOrientationLandscapeRight) )
{
//Landscape
}
else
{
//Portrait
}
Solution 5
Mix it up a little:
BOOL isLandscape = self.view.frame.size.width > self.view.frame.size.height;
(edit) Obviously the previous answers are the correct way to do this and this solution would fail in a situation where view controllers are not full-screen.
Comments
-
Daddy over 4 years
I don't need to specify the orientation in this case, I just need to detect it, but I'm having trouble. I have conditional code that should only work in portrait, and if the device is in landscape I need to do something else. Since the deviceOrientation is not necessarily the same as the interfaceOrientation, I can't come up with a way to test for portrait mode.
Most tutorials I find on Google are ways to force landscape or do some sort of rotation. The only thing I want to do is just determine what the orientation is. Here is my code, which is not working:
-(void)viewDidLoad { [super viewDidLoad]; //currentOrientation is declared as UIInterfaceOrientation currentOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; NSLog(@"%@",currentOrientation); // == NULL }
I need to determine the value of the interfaceOrientation and program conditionally. Thanks for your help!
-
Daddy about 14 yearsYour answer helps tremendously. Now all I have to test for is if isPortrait is true, instead of trying to add orientation code into the viewController rotation methods
-
M. Ryan about 14 yearsBe aware that this doesn't always seem to work. Particularly if your controller is the first in your application. In my simulator tests [self interfaceOrientation] always returns as 1. Maybe it's different on the actual device.. at the very least.. hard to test.
-
Joo Park about 14 yearsi believe when the app first start up, the default orientation is portrait. That might explain why [self interfaceOrientation] always returns 1.
-
Tirth almost 14 years@zoul, isPortrait bool value returning me "YES" in simulator. I have not iPad device, then how i detecting current device orientation in viewDidLoad?
-
Artilheiro almost 14 yearsthis does not work on detailViewController inside a split view... isPortrait is always YES. Any ideas?
-
Daredzik over 13 yearsUm, isn't that basically the exact same thing in the original question? I've found in my tests that it always returns 1 when the app inits, and is therefore pretty useless.
-
slycrel over 13 years@Matt, I think you're correct. No idea why I posted this back in may, it does indeed look like it's the same code as the original. I may have misunderstood the question, as I've used the above when the device orientation is unknown, face up or face down.
-
Sam Stewart over 13 yearsAwesome, really reliable on the sim. Thanks!
-
MusiGenesis about 13 yearsI just ran into this problem myself. The ultimate reason is that
self.interfaceOrientation
isNULL
when the view initially loads, so that bothUIDeviceOrientationIsPortrait
andUIDeviceOrientationIsLandscape
return NO. Using[UIApplication sharedApplication].statusBarOrientation
in the answer below always works correctly, since that orientation is never NULL. -
zmippie about 13 years@MusiGenesis: not true, as others have mentioned: it's often (in my case, always) 1 (portrait) when the app launches. There's no difference between interfaceOrientation in your (first) view controller or [UIApplication sharedApplication] either. It is really hard, if not, impossible to know the UI orientation at launch (device orientation isn't telling you anything when it's face up or down).
-
MusiGenesis about 13 years@zmippie: what isn't true, that self.interfaceOrientation is NULL in initial load or that the statusBarOrientation is never NULL? Those are both true for me.
-
Admin almost 13 yearsIt may be worth noting that it is not safe to assume statusBarOrientation is valid early on in execution. I appears as though this value is updated after didFinishLaunchingWithOptions exits. That is using iOS simulator 4.3.
-
Sam over 12 yearsI don't like using UIDeviceOrientation for orientations since when it is face up / face down it is more or less useless if you're trying to determine if your app is in landscape or portrait. This initially caused a bug in my code until I switched to using
[UIApplication sharedApplication].statusBarOrientation
. However, I wasn't trying to get the orientation during startup... Just my thoughts on this. -
AP inc. over 12 yearsuse [[UIDevice currentDevice] orientation] in viewDidLoad
-
ToddH over 11 yearsIf you're going to inspect self.interfaceOrientation you need to use UIInterfaceOrientationIsPortrait() instead of UIDeviceOrientationIsPortrait().
-
KDeogharkar about 11 yearsthis is very useful to me because first time run UIInterfaceOrientation orientation = (UIInterfaceOrientation)[UIDevice currentDevice].orientation; always return 0 . even if it is portrait.
-
barfoon almost 10 yearsinterfaceOrientation first deprecated in iOS 8.0
-
Chris Conover over 8 yearsaka UIInterfaceOrientationIsLandscape()
-
Jerry Chen almost 8 yearsUnfortunately, this quite good solution is deprecated in iOS 9
-
rockdaswift over 5 yearsSometimes you need the orientation of the device to rotate the camera input