How to check if device orientation is landscape le

2019-03-14 12:35发布

    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?

6条回答
【Aperson】
2楼-- · 2019-03-14 12:37

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")
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-14 12:38

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
 }
查看更多
贼婆χ
4楼-- · 2019-03-14 12:42

UIDeviceOrientation will return a value for that:

   enum UIDeviceOrientation : Int {
        case Unknown
        case Portrait
        case PortraitUpsideDown
        case LandscapeLeft
        case LandscapeRight
        case FaceUp 
        case FaceDown
    }
查看更多
可以哭但决不认输i
5楼-- · 2019-03-14 12:54

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")
    }
查看更多
【Aperson】
6楼-- · 2019-03-14 12:56

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-03-14 12:59

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 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 {

        } 
查看更多
登录 后发表回答