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

mafioso picture mafioso · Jul 28, 2016 · Viewed 37.5k times · Source
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        print("landscape")
    }
    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
        print("portrait")
    }

How can I check if it's landscape left or right?

Answer

Ketan Parmar picture Ketan Parmar · Jul 28, 2016

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 {

        }