Updated to Swift 4: Add the code below to ViewDidLoad :
NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: Notification.Name("UIDeviceOrientationDidChangeNotification"), object: nil)
Then create one function as below
@objc func orientationChanged() { if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){ print("landscape") } if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){ print("Portrait") } }
Hope this helps you :)
source share