How to get current iPhone orientation?

Is there a special method for targeting the iPhone? I don't need this in degrees or radians, I want it to return a UIInterfaceOrientation object. I just need this to build an if-else like

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) { //Code } if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) { //Code } 

Thanks in advance!

+41
objective-c accelerometer
Mar 25 '10 at 19:25
source share
4 answers

Most likely you want:

 UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

Then you can use system macros, for example:

 if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) { } 

If you want the device orientation to be used:

 UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

This includes enumerations of type UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown

+109
Mar 25 '10 at 19:36
source share

As discussed in other answers, you need an Orientation interface, not a deviceOrientation.

The easiest way to get to this is to use the Orientation property interface on your UIViewController. (Therefore, most often it is simple: self.interfaceOrientation will do).

Possible values:

 UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft 

Remember: Left orientation is entered by turning the device to the right.

+11
Feb 07 2018-12-12T00:
source share

Here is a snippet of code that I wrote because I was having strange problems returning to my root view when the orientation was changed ... I see that you are just calling a method that should have been called, but seemed to be ... . this works fine without errors

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){ //do something or rather [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft]; NSLog(@"landscape left"); } if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){ //do something or rather [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight]; NSLog(@"landscape right"); } if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){ //do something or rather [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]; NSLog(@"portrait"); } } 
+4
Apr 16 2018-12-12T00:
source share

UIInterfaceOrientation now deprecated, and UIDeviceOrientation includes UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown , so you cannot rely on the orientation of the interface.

The solution is pretty simple though

 if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) { // Landscape } else { // Portrait } 
+1
Dec 10 '16 at 4:12
source share



All Articles