ios 如何快速检查设备方向是横向左侧还是右侧?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38629782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if device orientation is landscape left or right in swift?
提问by mafioso
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?
我怎么能检查它是左边还是右边的风景?
回答by Ketan Parmar
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
快速 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
斯威夫特 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 {
}
回答by xlsmearlx
This works on Swift 3 & 4
这适用于 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
}
回答by Nosov Pavel
There is one thing that destroy all this answers - it's iOS9 iPad multitasking.
有一件事破坏了所有这些答案——它是 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).
在 iOS 9 上,iPad 应用程序默认选择 iPad 多任务处理。这意味着它必须始终采用所有方向。由于您没有选择退出 iPad 多任务处理,因此运行时假定您始终采用所有方向 — 因此它无需费心询问您允许哪些方向,因为它已经知道答案(所有这些方向) )。
To do right cell size for UICollectionView I do next :
要为 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))
}
}
and inside viewWillTransition next:
然后在 viewWillTransition 里面:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
layout.invalidateLayout()
}
}
because it works faster than without.
因为它比没有工作得更快。
回答by Westside
UIDeviceOrientation will return a value for that:
UIDeviceOrientation 将为此返回一个值:
enum UIDeviceOrientation : Int {
case Unknown
case Portrait
case PortraitUpsideDown
case LandscapeLeft
case LandscapeRight
case FaceUp
case FaceDown
}
回答by budidino
Swift 3+
斯威夫特 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")
}
回答by Heretic Sic
SWIFT 4.2
斯威夫特 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")
}
回答by Marjan Basiri
Swift 5.0:
斯威夫特 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).
isFlat:指示指定的方向是面朝上还是面朝下(设备与地面平行,屏幕朝下或不朝下)。
isValidInterfaceOrientation:Indicating whether the specified orientation is one of the portrait or landscape orientations.
isValidInterfaceOrientation:指示指定的方向是纵向还是横向之一。
IMPORTANT NOTE:
重要的提示:
If you have set some views to landscape form manually, so "isLandscape" value is not correct.
如果您手动将某些视图设置为横向形式,则“isLandscape”值不正确。
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.
例如,我想以横向形式播放所有视频,而我的应用程序仅用于纵向形式,因此在视频视图中,我使用此条件来检查视图是否为横向,与手机方向无关。