Swift 5 answer
Subscribe to notifications on orientations changes:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(orientationChangedFromNotification), name: UIDevice.orientationDidChangeNotification, object: nil)
}
Handle notification:
@objc func orientationChangedFromNotification() {
self.updateUIForOrientation()
}
Handle orientation changes:
func updateUIForOrientation() {
let orientation = UIDevice.current.orientation
//TODO: Your logic
}
Note: This will tell you the orientation of the physical device. It is also possible to access the interface orientation of the app:
UIApplication.shared.delegate?.window?.windowScene?.interfaceOrientation