Get current orientation of iPad?

2019-01-10 05:46发布

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.

11条回答
Fickle 薄情
2楼-- · 2019-01-10 06:37

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!

查看更多
成全新的幸福
3楼-- · 2019-01-10 06:39

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

查看更多
地球回转人心会变
4楼-- · 2019-01-10 06:41

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

查看更多
Explosion°爆炸
5楼-- · 2019-01-10 06:42

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.

查看更多
地球回转人心会变
6楼-- · 2019-01-10 06:43

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

查看更多
登录 后发表回答