I have an issue in one of my applications that I just discovered once I got the app onto an actual iPad, it isn't possible to see in the simulator. The issue is when I hold the iPad in landscape orientation, if I tip the iPad back to a certain angle the iPad stays in landscape mode but the view switches to my portraitView while still in landscape mode. In my code I have a function called screenRotated() that is an observer of UIDeviceOrientationDidChangeNotification. My function screenRotated() has the following code:
let interfaceOrientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
if UIDeviceOrientationIsLandscape(interfaceOrientation) {
//does some stuff and then sets self.view = landscapeView
} else {
//does some stuff and then sets self.view = portraitView
}
How do I keep my app from going into the wrong view when in landscape orientation?
You issue will be that you are not handling device orientation notifications for orientations UIDeviceOrientationFaceUp
and UIDeviceOrietationFaceDown
. These are neither Portrait or Landscape and your code always picks Portrait when the orientation is not Landscape.
Hence as you are tipping back, it goes to orientation face up and your code picks Portrait as it is not landscape but face up.
So add code to detect faceup/down and ideally keep to the orientation last set until you see it actually go from Portrait to Landscape or the other way around.
The following should work:
if UIDeviceOrientationIsLandscape(interfaceOrientation) {
//does some stuff and then sets self.view = landscapeView
} else if UIDeviceOrientationIsPortrait(interfaceOrientation) {
//does some stuff and then sets self.view = portraitView
}
else{
// Do nothing
}