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条回答
我想做一个坏孩纸
2楼-- · 2019-01-10 06:18

One of:

  • Check the interfaceOrientation property of the active view controller.
  • [UIApplication sharedApplication].statusBarOrientation.
  • [UIDevice currentDevice].orientation. (You may need to call -beginGeneratingDeviceOrientationNotifications.)
查看更多
仙女界的扛把子
3楼-- · 2019-01-10 06:23

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

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation);
查看更多
Emotional °昔
4楼-- · 2019-01-10 06:27

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

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-10 06:34

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
查看更多
再贱就再见
6楼-- · 2019-01-10 06:34

[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.

查看更多
再贱就再见
7楼-- · 2019-01-10 06:35

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.

查看更多
登录 后发表回答