How to check if device orientation is landscape left or right in swift? How to check if device orientation is landscape left or right in swift? ios ios

How to check if device orientation is landscape left or right in swift?


you can do something like,

if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft{}else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight{}else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown{}else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortrait{}

SWIFT 5

    if UIDevice.current.orientation.isLandscape {    } else if UIDevice.current.orientation.isFlat {    } else if UIDevice.current.orientation.isPortrait {    } else if UIDevice.current.orientation.isValidInterfaceOrientation {    }

SWIFT 3

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {        } 


This works on Swift 3 & 4

switch UIApplication.shared.statusBarOrientation {    case .portrait:        //do something        break    case .portraitUpsideDown:        //do something        break    case .landscapeLeft:    //do something        break    case .landscapeRight:        //do something        break    case .unknown:        //default        break }


Swift 5.0:

UIDevice.current.orientation.isLandscape UIDevice.current.orientation.isFlatUIDevice.current.orientation.isPortraitUIDevice.current.orientation.isValidInterfaceOrientation

isFlat:Indicating whether the specified orientation is face up or face down (The device is held parallel to the ground with the screen facing downwards or Not).

isValidInterfaceOrientation:Indicating whether the specified orientation is one of the portrait or landscape orientations.

IMPORTANT NOTE:

If you have set some views to landscape form manually, so "isLandscape" value is not correct.

In this case you can use this condition:

if UIScreen.main.bounds.height < UIScreen.main.bounds.width {    print("LandScape Views")    print("Portrait Views")}

For example I wanted to play all videos in landscape form while my app was only for portrait form, so in video views I used this condition in order to check if the view is landscape or not, independent of the phone orientation.