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.
相关问题
- how do you prevent page scroll in textarea on mobi
- How to change the backgroundColor for all UITableV
- The bundle identifier cannot be changed from the c
- execute method when iPhone enters landscape mode
- iPad Landscape messing up touches began
相关文章
- How do you detect key up / key down events from a
- Loss of variables switching orientations
- Can I release an app without the device?
- Flutter Camera Preview not rotating with phone ori
- Universal iPad App rejected because of launch cras
- What does the “Use for Development” button in Xcod
- Does the iOS-Simulator use multiple cores?
- iPad modal form sheet takes up the whole screen an
One of:
interfaceOrientation
property of the active view controller.[UIApplication sharedApplication].statusBarOrientation
.[UIDevice currentDevice].orientation
. (You may need to call-beginGeneratingDeviceOrientationNotifications
.)For determining landscape vs portrait, there is a built-in function:
[UIApplication sharedApplication].statusBarOrientation
returns portrait when it's landscape, and landscape when it's portrait at launch, in iPadOrientation 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:Alternatively, you can request to receive orientation change notifications:
Some people also like to check the status bar orientation:
[UIDevice currentDevice].orientation
works great.BUT!!! ... the trick is to add it to -
(void)viewWillAppear:(BOOL)animated
exp:
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.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.
Then, any place where you need to check the orientation you can do something like this:
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.