Get current orientation of iPad?

IpadOrientation

Ipad Problem Overview


In a given event handler (not the "shouldAutorotateToInterfaceOrientation" method) how do I detect the current iPad orientation? I have a text field I have to animate up (when keyboard appears) in the Landscape view, but not in the portrait view and want to know which orientation I'm in to see if the animation is necessary.

Ipad Solutions


Solution 1 - Ipad

Orientation information isn't very consistent, and there are several approaches. If in a view controller, you can use the interfaceOrientation property. From other places you can call:

[[UIDevice currentDevice] orientation]

Alternatively, you can request to receive orientation change notifications:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

Some people also like to check the status bar orientation:

[UIApplication sharedApplication].statusBarOrientation

Solution 2 - Ipad

I think

[[UIDevice currentDevice] orientation];

is not really reliable. Sometimes it works, sometimes not... In my apps, I use

[[UIApplication sharedApplication]statusBarOrientation]; 

and it works great!

Solution 3 - Ipad

One of:

  • Check the interfaceOrientation property of the active view controller.
  • [UIApplication sharedApplication].statusBarOrientation.
  • [UIDevice currentDevice].orientation. (You may need to call -beginGeneratingDeviceOrientationNotifications.)

Solution 4 - Ipad

I found a trick to solve the FaceUp orientation issue!!!

Delay the orientation check till AFTER the app has started running, then set variables, view sizes, etc.!!!

//CODE

- (void)viewDidLoad {

  [super viewDidLoad];

  //DELAY
  [NSTimer scheduledTimerWithTimeInterval:0.5 
		             target:self 
			         selector:@selector(delayedCheck) 
					 userInfo:nil 
					 repeats:NO];

}


-(void)delayedCheck{

  //DETERMINE ORIENTATION
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
      FACING = @"PU";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
      FACING = @"PD";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
      FACING = @"LL";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
      FACING = @"LR";
  }	
  //DETERMINE ORIENTATION

  //START
  [self setStuff];
  //START

}


-(void)setStuff{

  if( FACING == @"PU" ){
          //logic for Portrait
  }
  else
  if( FACING == @"PD" ){
          //logic for PortraitUpsideDown
  }
  else{	
  if( FACING == @"LL"){
          //logic for LandscapeLeft
  }
  else
  if( FACING == @"LR" ){
          //logic for LandscapeRight
  }

}

//CODE

You can addSubviews, position elements, etc. in the 'setStuff' function ... anything that would initially depend on the orientation!!!

:D

-Chris Allinson

Solution 5 - Ipad

You can achieve this by two ways:

1- By using the following method:

**Put the following line in the -(void)viewDidLoad Method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];

then put this method inside your class

-(void)deviceRotated:(NSNotification*)notification
{

   UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        //Do your textField animation here
    }
}

The above method will check the orientation when the device will be rotated

2- The second way is by inserting the following notification inside -(void)viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

then put the following method inside your class

-(void)checkRotation:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
         //Do your textField animation here
    }
}

The above method will check the orientation of the status bar of the ipad or iPhone and according to it you make do your animation in the required orientation.

Solution 6 - Ipad

For determining landscape vs portrait, there is a built-in function:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation);

Solution 7 - Ipad

[UIApplication sharedApplication].statusBarOrientation returns portrait when it's landscape, and landscape when it's portrait at launch, in iPad

Solution 8 - Ipad

I don't know why, but every time my app starts, the first 4 are right, but subsequently I get the opposite orientation. I use a static variable to count this, then have a BOOL to flip how I manually send this to subviews.

So while I'm not adding a new stand-alone answer, I'm saying use the above and keep this in mind. Note: I'm receiving the status bar orientation, as it's the only thing that gets called when the app starts and is "right enough" to help me move stuff.

The main problem with using this is the views being lazily loaded. Be sure to call the view property of your contained and subviews "Before" you set their positions in response to their orientation. Thank Apple for not crashing when we set variables that don't exist, forcing us to remember they break OO and force us to do it, too... gah, such an elegant system yet so broken! Seriously, I love Native, but it's just not good, encourages poor OO design. Not our fault, just reminding that your resize function might be working, but Apple's Way requires you load the view by use, not by creating and initializing it

Solution 9 - Ipad

In your view controller, get the read-only value of self.interfaceOrientation (the current orientation of the interface).

Solution 10 - Ipad

I've tried many of the above methods, but nothing seemed to work 100% for me.

My solution was to make an iVar called orientation of type UIInterfaceOrientation in the Root View Controller.

- (void)viewDidLoad {
    
    [super viewDidLoad];
    orientation = self.interfaceOrientation; // this is accurate in iOS 6 at this point but not iOS 5; iOS 5 always returns portrait on app launch through viewDidLoad and viewWillAppear no matter which technique you use.
}
 

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    orientation =  toInterfaceOrientation;

}

Then, any place where you need to check the orientation you can do something like this:

 if(UIInterfaceOrientationIsPortrait(orientation)){
    // portrait
  }else{
   // landscape
  }

There may still be a better way, but this seems to work 98% of the time (iOS5 notwithstanding) and isn't too hard. Note that iOS5 always launches iPad in portrait view, then sends a device the willRotateTo- and didRotateFromInterfaceOrientation: messages, so the value will still be inaccurate briefly.

Solution 11 - Ipad

[UIDevice currentDevice].orientation works great.

BUT!!! ... the trick is to add it to - (void)viewWillAppear:(BOOL)animated

exp:

(void)viewWillAppear:(BOOL)animated{
   ...
   BOOL isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);
   ...
}

If you call it at - (void)viewDidLoad, it does not work reliable, especially if you use multiple threads (main UI thread, background thread to access massive external data, ...).


Comments:

  1. Even if your app sets default orientation portrait, user can lock it at landscape. Thus setting the default is not really a solution to work around it.
  2. There are other tasks like hiding the navigation bar, to be placed at viewWillAppear to make it work and at the same time prevent flickering. Same applies to other views like UITableView willDisplayCell -> use it to set cell.selected and cell.accessoryType.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeNView Question on Stackoverflow
Solution 1 - IpadPaul LynchView Answer on Stackoverflow
Solution 2 - IpadSamuelView Answer on Stackoverflow
Solution 3 - IpadkennytmView Answer on Stackoverflow
Solution 4 - IpadChris AllinsonView Answer on Stackoverflow
Solution 5 - IpadAhmed HammadView Answer on Stackoverflow
Solution 6 - IpadcalebView Answer on Stackoverflow
Solution 7 - IpadFiras KADHUMView Answer on Stackoverflow
Solution 8 - IpadStephen JView Answer on Stackoverflow
Solution 9 - IpadMarcoView Answer on Stackoverflow
Solution 10 - IpadavanceView Answer on Stackoverflow
Solution 11 - IpadMichael BiermannView Answer on Stackoverflow