'UIDeviceOrientation giving landscape orientation in Portrait mode in Swift
I want to check device orientation, where my device is in Portrait mode, and app is in Landscape. But my code sometimes gives Landscape. Here is my code:
if UIDevice.current.orientation.isLandscape {
print("Landscape")
}
else {
print("Portrait")
}
This is intermittent, but it mostly happens where it goes into Landscape, even when my device is in Portrait mode. Not sure if it is giving orientation of the app or device.
Solution 1:[1]
orientation returns the current orientation of your device not the orientation of the user interface of your application.
This is what documentation says.
The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface.
In addition to that there are more vales for the orientation like faceUp, faceDown. So in your if-else statement your are grouping every other value except landscape as portrait. Be careful this might also create some unexpected outputs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
