Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    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?

like image 534
mafioso Avatar asked Jul 28 '16 07:07

mafioso


People also ask

What is landscape left and right?

LandscapeLeft: The device is in landscape mode, with the device held upright and the home button on the right side. LandscapeRight: The device is in landscape mode, with the device held upright and the home button on the left side. Add comment · Share.

How can we detect the orientation of the screen?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });


2 Answers

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 {          }  
like image 183
Ketan Parmar Avatar answered Oct 10 '22 02:10

Ketan Parmar


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  } 
like image 36
xlsmearlx Avatar answered Oct 10 '22 01:10

xlsmearlx