I'm determining a frame offset for a keyboard if the user is holding iPad in landscape mode. I'm finding that I'm unable to properly set the offset if I'm adding content in landscape mode and then set the iPad flat on a surface. I'm using the code below to set the offset:
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
tableView.frame = CGRectMake(0,0,tableView.frame.size.width, tableView.frame.size.height-keyboardsize.height);
}
UIDeviceOrientations that I can check for are:
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
Is there any way for me to check/know if a user has been holding the app in landscape orientation (UIDeviceOrientationLandscapeLeft
or UIDeviceOrientationLandscapeRight
) and then set the device down (which should be UIDeviceOrientationFaceUp
)
What you're interested in is not the device orientation, but the interface orientation. Try using self.interfaceOrientation
(where self
is your view controller). You can also get this from [[UIApplication sharedApplication] statusBarOrientation]
.
That being said, you probably shouldn't be using the interface orientation at all to find out where the keyboard is. The keyboard generates notifications when it appears and moves around, which include position information. Take a look at the UIKeyboardWillShowNotification
and UIKeyboardWillChangeFrameNotification
. They have position information in their userInfo dictionary under the UIKeyboardFrameBeginUserInfoKey
and UIKeyboardFrameEndUserInfoKey
keys which will tell you where the keyboard will be.