【问题标题】:How to check if device orientation is landscape left or right in swift?如何快速检查设备方向是横向还是横向?
【发布时间】:2016-07-28 07:30:02
【问题描述】:
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        print("landscape")
    }
    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
        print("portrait")
    }

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

【问题讨论】:

标签: ios swift device-orientation


【解决方案1】:

你可以做类似的事情,

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 {

        } 

【讨论】:

  • 这不考虑设备界面为纵向模式但设备平放的情况
  • 一个 switch 比许多 if 语句更好。
【解决方案2】:

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

【讨论】:

  • 您不需要在 Swift 中使用 break,因为 switch 不会通过它的情况。
  • @Moritz 实际上他需要在这种特殊情况下输入break,因为如果case 中没有代码行,编译器会抛出错误。因此将break 放入case 是让编译器关闭的最简单方法。
【解决方案3】:

斯威夫特 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")
}

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

【讨论】:

    【解决方案4】:

    UIDeviceOrientation 将为此返回一个值:

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

    【讨论】:

      【解决方案5】:

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

      在 iOS 9 上,iPad 应用程序默认选择 iPad 多任务处理。这意味着它必须始终采用所有方向。由于您没有选择退出 iPad 多任务处理,运行时假定您确实始终采用所有方向 - 因此它不需要费心询问您允许哪些方向,因为它已经知道答案(所有这些)。

      接下来要为 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()
              }
      }
      

      因为它比没有它运行得更快。

      【讨论】:

        【解决方案6】:

        斯威夫特 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")
        }
        

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-30
            • 2012-08-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多