I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode
- (void)viewDidAppear:(BOOL)animated
{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
NSLog(@"orientation %d", orientation);
if ((orientation == 2) || (orientation == 3) || (orientation == 4))
{
[self performSegueWithIdentifier:@"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
}
With the iPhone 6 Plus, 6s Plus, and 7s Plus, you have the ability to use the home screen in landscape mode (with the iPhone held horizontally) or in portrait mode (the iPhone held vertically).
Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.
The interfaceOrientation
property is deprecated since iOS 8.
And the helpers methods
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
won't help either, because they return false when orientation is .faceUp
.
So I ended checking this way:
extension UIViewController {
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or faceDown
guard let window = self.view.window else { return false }
return window.frame.size.width < window.frame.size.height
}
}
}
This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.
I use window
because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.
In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.
The most important difference for your case is that the interface orientation is never face up/down.
In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation
.
Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)
Bear in mind that a device orientation landscape left means a user interface orientation landscape right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With