37
    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?

mafioso
  • 1,630
  • 4
  • 25
  • 45
  • 1
    Possible duplicate of [Getting device orientation in Swift](http://stackoverflow.com/questions/25796545/getting-device-orientation-in-swift) – HariKrishnan.P Jul 28 '16 at 07:32
  • Possible duplicate of [How to programmatically determine iPhone interface orientation?](http://stackoverflow.com/questions/634745/how-to-programmatically-determine-iphone-interface-orientation) – Rajan Maheshwari Jul 28 '16 at 07:34

7 Answers7

70

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 {

        } 
Luckabo
  • 74
  • 5
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • 17
    this doesn't take into account when the device's interface is in portrait mode but the device is laid out flat – shoe Feb 15 '17 at 05:02
  • 3
    A switch would be a much better way of doing this than lots of if statements. – Fogmeister Sep 17 '18 at 09:12
11

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
 }
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
xlsmearlx
  • 628
  • 8
  • 9
  • 3
    You don't need to `break` in Swift because `switch` does not fallthrough its cases. – Eric Aya Aug 15 '17 at 14:49
  • 3
    @Moritz Actually he needs to put `break` in this particular case because compiler will throw an error if there're no code lines inside `case`. So putting `break` into `case` is an easiest way to make compiler shut up. – Eugene Alexeev Sep 17 '18 at 09:07
5

Swift 5.0:

UIDevice.current.orientation.isLandscape 
UIDevice.current.orientation.isFlat
UIDevice.current.orientation.isPortrait
UIDevice.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.

Marjan Basiri
  • 184
  • 1
  • 10
3

UIDeviceOrientation will return a value for that:

   enum UIDeviceOrientation : Int {
        case Unknown
        case Portrait
        case PortraitUpsideDown
        case LandscapeLeft
        case LandscapeRight
        case FaceUp 
        case FaceDown
    }
Westside
  • 675
  • 1
  • 7
  • 16
3

There is one thing that destroy all this answers - it's iOS9 iPad multitasking.

On iOS 9, an iPad app by default opts into iPad multitasking. This means that it must adopt all orientations at all times. Since you have not opted out of iPad multitasking, the runtime assumes that you do adopt all orientations at all times — and thus it doesn't need to bother to ask you what orientations you permit, as it already knows the answer (all of them).

To do right cell size for UICollectionView I do next :

func collectionView(_ collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if UIDevice.current.userInterfaceIdiom == .phone {
            return CGSize(width: collectionView.frame.size.width, height: 120)
        } else {
            var width:CGFloat = 0
            let height = 250
            // because of iOS9 and iPad multitasking
            if (UIApplication.shared.statusBarOrientation.rawValue <= UIInterfaceOrientation.portraitUpsideDown.rawValue) {
                width = (collectionView.frame.size.width - 3) / 3
            } else {
                width = (collectionView.frame.size.width - 4) / 4
            }
            return CGSize(width: Int(width), height: Int(height))
        }
    }

and inside viewWillTransition next:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.invalidateLayout()
        }
}

because it works faster than without.

Nosov Pavel
  • 1,571
  • 1
  • 18
  • 34
1

Swift 3+

switch UIDevice.current.orientation {
case .faceUp:
  print("flat - up")
case .faceDown:
  print("flat - down")
case .landscapeLeft:
  print("landscape - left")
case .landscapeRight:
  print("landscape - right")
case .portrait:
  print("portrait - normal")
case .portraitUpsideDown:
  print("portrait - upsideDown")
case .unknown:
  print("unknown")
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
1

SWIFT 4.2

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
                print("Landscape Left")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
                print("Landscape Right")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown{
               print("Portrait Upside Down")
            } 
    else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
                print("Portrait")
    }
Heretic Sic
  • 364
  • 2
  • 9