如何在 swift 中检查设备方向是向左还是向右?

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")
    }

如何检查它是横向还是横向?

你可以这样做,

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 {

        } 

UIDeviceOrientation 将 return 的值:

   enum UIDeviceOrientation : Int {
        case Unknown
        case Portrait
        case PortraitUpsideDown
        case LandscapeLeft
        case LandscapeRight
        case FaceUp 
        case FaceDown
    }

这适用于 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
 }

有一件事破坏了所有这些答案 - 它是 iOS9 iPad 多任务处理。

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).

要为 UICollectionView 设置合适的单元格大小,我接下来会做:

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))
        }
    }

接下来在 viewWillTransition 中:

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

因为它比没有它工作得更快。

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")
}

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")
    }

Swift 5.0:

UIDevice.current.orientation.isLandscape 
UIDevice.current.orientation.isFlat
UIDevice.current.orientation.isPortrait
UIDevice.current.orientation.isValidInterfaceOrientation

isFlat: 指示指定方向是正面朝上还是正面朝下(设备与地面平行,屏幕朝下或不)。

isValidInterfaceOrientation: 指示指定方向是纵向还是横向之一。

重要提示:

如果您手动将某些视图设置为横向,则"isLandscape"值不正确。

在这种情况下你可以使用这个条件:

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

例如,我想以横向形式播放所有视频,而我的应用仅适用于纵向形式,因此在视频视图中,我使用此条件来检查视图是否为横向,与 phone方向。