ios 如何查看 iPhone 现在处于哪个位置(横向或纵向)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2516491/
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 in which position (landscape or portrait) is the iPhone now?
提问by Knodel
I have an app with a tab bar, and nav controllers in each tab. When user shakes the device, a UIImageView
appears as a child view in the nav controller. But the UIImageView
must contain a special image, depending on the device's current orientation.
我有一个带有标签栏的应用程序,每个标签中都有导航控制器。当用户摇动设备时,a 会UIImageView
在导航控制器中显示为子视图。但是UIImageView
必须包含一个特殊的图像,这取决于设备的当前方向。
If I write just
如果我只写
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//Code
}
}
The view just goes crazy if user rotated the device before shaking.
如果用户在晃动之前旋转设备,视图就会变得疯狂。
Is there a method to get iPhones current orientation?
有没有办法让 iPhone 的当前方向?
采纳答案by Tim
回答by Azhar
Here are macros UIDeviceOrientationIsLandscape
and UIDeviceOrientationIsPortrait
这里是宏UIDeviceOrientationIsLandscape
和UIDeviceOrientationIsPortrait
so rather checking separately you can do it like this ...
所以宁愿单独检查,你可以这样做......
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
OR
或者
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
回答by Jim True
As beno said, this seems a better answer if you are detecting orientation early in your View. I couldn't get the approved answer to return a result early in my setup but this works wonderfully.
正如 beno 所说,如果您在视图中尽早检测方向,这似乎是一个更好的答案。我无法在我的设置早期获得批准的答案以返回结果,但这非常有效。
if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
//DO Portrait
}else{
//DO Landscape
}
回答by Shirkrin
To addon to the already answered question:
补充已经回答的问题:
You use [[UIDevice currentDevice] orientation]
which will yield one of these values:
您可以使用[[UIDevice currentDevice] orientation]
这将产生这些值之一:
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
The documentation can be found here - (orientation)and here - (UIDeviceOrientation).
文档可以在这里找到- (orientation)和这里 - (UIDeviceOrientation)。
(I don't mean to claim the former anwser, but this information was to big for a comment.)
(我并不是要声称以前的 anwser,但此信息对于评论来说太大了。)
回答by Varun
Try this:
尝试这个:
[[UIApplication sharedApplication] statusBarOrientation]
Or in Swift 3:
或者在 Swift 3 中:
UIApplication.shared.statusBarOrientation
To specifically check for a particular orientation you can also try the isLandscape
or isPortrait
property like so:
要专门检查特定方向,您还可以尝试使用isLandscape
orisPortrait
属性,如下所示:
UIApplication.shared.statusBarOrientation.isLandscape
The problem with [[UIDevice currentDevice] orientation]
is that it will also return UIInterfaceOrientationUnknown
which statusBarOrientation
does not.
问题[[UIDevice currentDevice] orientation]
在于它也会返回UIInterfaceOrientationUnknown
,而statusBarOrientation
不会返回。
There is also a UIViewController
property interfaceOrientation
but it was deprecated in iOS 8, so it's not recommended.
还有一个UIViewController
属性,interfaceOrientation
但它在 iOS 8 中已被弃用,因此不推荐使用。
You check documentation for statusBarOrientation
here
您在此处查看文档statusBarOrientation
回答by beno
You can also use the interfaceOrientation property of the UIViewController class, if you are stuck and continuously getting UIDeviceOrientationUnknown from UIDevice.
如果您卡住并不断从 UIDevice 获取 UIDeviceOrientationUnknown,您还可以使用 UIViewController 类的 interfaceOrientation 属性。
There's a good summary of why [[UIDevice currentdevice] orientation] can sometimes fail here: http://bynomial.com/blog/?p=25, especially if you want to detect the orientation quickly (for example, if you wanted to check right when the app comes out of the background).
有一个很好的总结为什么 [[UIDevice currentdevice] 方向] 有时会在这里失败:http: //bynomial.com/blog/?p=25,特别是如果你想快速检测方向(例如,如果你想当应用程序退出后台时检查正确)。
回答by Mohamed Nazir
It helps you...
它可以帮助你...
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSLog(@"Lanscapse");
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(@"UIDeviceOrientationPortrait");
}
}
回答by Dhaval Gevariya
As Simple Solution of Swift 4.2
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) { switch UIDevice.current.orientation{ case .portrait: print("Portrait") case .portraitUpsideDown: print("PortraitUpsideDown") case .landscapeLeft: print("LandscapeLeft") case .landscapeRight: print("LandscapeRight") default: print("Another") } }
作为 Swift 4.2 的简单解决方案
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) { switch UIDevice.current.orientation{ case .portrait: print("Portrait") case .portraitUpsideDown: print("PortraitUpsideDown") case .landscapeLeft: print("LandscapeLeft") case .landscapeRight: print("LandscapeRight") default: print("Another") } }
回答by Beninho85
You can also define constants to earn time :
您还可以定义常量来赚取时间:
#define LANDSCAPE UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
#define LANDSCAPE_RIGHT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft
#define LANDSCAPE_LEFT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight
#define PORTRAIT UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
#define PORTRAIT_REVERSE [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown
回答by Frédéric Adda
You could check it like so (Swift 3):
你可以像这样检查它(Swift 3):
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .faceUp, .faceDown:
// Check the interface orientation
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation{
case .portrait, .portraitUpsideDown:
return true
default:
return false
}
default: // .unknown
return false // not very satisfying to return false as if we were in landscape :-/
}
}
If you are in a ViewController, you can also do like so (that is what I ended up doing):
如果您在 ViewController 中,您也可以这样做(这就是我最终做的):
private var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or FaceDown
return self.view.width < self.view.height
}
}
Although even this should be enough in that case:
尽管在这种情况下,即使这样也应该足够了:
private var isPortrait: Bool {
return self.view.width < self.view.height
}