if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
print("landscape")
}
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
print("portrait")
}
如何检查它的横向是左还是右?
答案 0 :(得分:41)
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 {
}
答案 1 :(得分:6)
这适用于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
}
答案 2 :(得分:3)
UIDeviceOrientation将返回一个值:
enum UIDeviceOrientation : Int {
case Unknown
case Portrait
case PortraitUpsideDown
case LandscapeLeft
case LandscapeRight
case FaceUp
case FaceDown
}
答案 3 :(得分:3)
有一件事会破坏所有这些答案 - 这是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()
}
}
因为它比没有它更快。
答案 4 :(得分: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")
}
答案 5 :(得分:0)
快速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")
}
答案 6 :(得分:0)
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")
}
例如,我想以横向格式播放所有视频,而我的应用程序仅以纵向格式播放,因此在视频视图中,我使用此条件来检查视图是否横向(独立于手机方向)。< / p>