Heres a sample code that will print if the device is "portrait" or "landscape" when it first launches and also when it detects an orientation change.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getOrientation()
}
func getOrientation() {
let orientation = UIDevice.current.orientation
if orientation == .landscapeLeft || orientation == .landscapeRight {
print("landscape")
}
else {
print("portrait")
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
getOrientation()
}
}
If I launch the app with the phone portrait, it works fine. However, if I launch the app with the phone in landscape, it'll detect portrait at first. But if you rotate the device afterward it'll be correct. Is this a bug or am I using UIDevice.current.orientation
incorrectly?