Get current iPad orientation?

In this event handler (and not in the "shouldAutorotateToInterfaceOrientation" method), how do I determine the current orientation of the iPad? I have a text box that I have to animate (when the keyboard appears) in the Landscape view, but not in portrait mode, and I want to know what orientation I want to see if animation is needed.

+62
ipad orientation
Apr 29 '10 at 15:49
source share
11 answers

Orientation information is not very consistent, and there are several approaches. If in the 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 
+132
Apr 29 '10 at 16:00
source share

I think,

 [[UIDevice currentDevice] orientation]; 

not very reliable. Sometimes it works, sometimes not ... In my applications I use

 [[UIApplication sharedApplication]statusBarOrientation]; 

and it works great!

+34
Jul 04 2018-11-11T00:
source share

One of:

  • Check the interfaceOrientation property of the active view controller.
  • [UIApplication sharedApplication].statusBarOrientation .
  • [UIDevice currentDevice].orientation . (You may need to call -beginGeneratingDeviceOrientationNotifications .)
+9
Apr 29 '10 at 15:58
source share

I found a trick to solve FaceUp orientation problem !!!

Complete the orientation check until AFTER the application starts, then set the variables, view the 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 

In the function 'setStuff' you can add Subviews, position elements, etc ... everything that initially depends on the orientation !!!

: D

- Chris Allinson

+8
May 03 '11 at 17:31
source share

You can achieve this in two ways:

1- Using the following method:

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

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

then put this method in 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 is rotated

2- The second way is to insert the following notification inside -(void)viewDidLoad

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

then enter 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 in accordance with this you make the animation in the desired orientation.

+3
Nov 13 '12 at 7:36
source share

To determine the landscape and portrait there is a built-in function:

 UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation); 
+2
May 08 '12 at 22:35
source share

[UIApplication sharedApplication].statusBarOrientation returns a portrait when it is a landscape, and a landscape when it is a portrait at startup, on the iPad

+1
Feb 12 '13 at 16:15
source share

I do not know why, but every time my application starts, the first 4 are right, but subsequently I get the opposite orientation. I use a static variable to read this, and then BOOL to flip over, as I manually send this to subviews.

Therefore, when I do not add a new standalone answer, I say that I use the foregoing and remember this. Note. I get the status bar orientation, as this is the only thing that gets called when the application starts and is โ€œtrue enoughโ€ to help me move stuff.

The main problem with using this is lazily loaded views. Be sure to call the view property of your content and the "Before" sub-items, you will set your positions in response to their orientation. Thanks to Apple for not crashing when we set variables that don't exist, making us remember that they break OO and make us do it ... gah, such an elegant system, but so broken! Seriously, I love Native, but it's just not good, it encourages poor OO design. Not our mistake, just a reminder that your resize function may work, but Apple Way requires you to load the view with, rather than create and initialize it

0
Mar 09 '12 at 17:25
source share

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

0
Nov 16
source share

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

My solution was to make an iVar, called an orientation of the UIInterfaceOrientation type 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, anywhere 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 it works 98% of the time (despite iOS5) and not too complicated. Note that iOS5 always launches the iPad in portrait mode, then sends willRotateTo and didRotateFromInterfaceOrientation: messages to the device, so the value will still be inaccurate.

0
Apr 05 '13 at 22:44
source share

[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 on - (void)viewDidLoad , it does not work reliably, especially if you use multiple threads (main UI thread, background thread to access massive external data, ...).




Comments: 1) Even if your application sets the default orientation portrait, the user can lock it in landscape mode. Thus, the default setting is not a solution for its operation. 2) There are other tasks, such as hiding the navigation bar, which must be placed in viewWillAppear to make it work and at the same time prevent flickering. The same applies to other views, such as UITableView willDisplayCell - willDisplayCell use it to set cell.selected and cell.accessoryType.

-one
Apr 26 2018-12-12T00:
source share



All Articles