如何检查iPhone现在的位置(横向或纵向)?

Kno*_*del 36 objective-c ios

我有一个带标签栏的应用程序,每个标签中都有导航控制器.当用户摇动设备时,UIImageView在导航控制器中显示为子视图.但UIImageView必须包含特殊图像,具体取决于设备的当前方向.

如果我写的只是

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
   if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
   //Code
   }
   else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
   //Code
   }
}
Run Code Online (Sandbox Code Playgroud)

如果用户在摇动前旋转设备,视图就会变得疯狂.

有没有办法让iPhone出现当前定位?

Azh*_*har 49

这是宏UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait

所以相反单独检查你可以这样做......

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
    // code for landscape orientation      
}
Run Code Online (Sandbox Code Playgroud)

要么

 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
 {
     // code for Portrait orientation       
 }
Run Code Online (Sandbox Code Playgroud)

  • 我注意到,如果应用程序启动并且尚未更改方向,则这些值不会返回正确的值.有没有办法解决? (3认同)

Tim*_*Tim 35

使用此处[[UIDevice currentDevice] orientation]指定的方法.

  • 如果您不关心*设备的*方向,而只关心*接口的*方向,您可以向当前的UIViewController询问其`-interfaceOrientation`.它没有未知值,因此您将始终获得定义明确的内容,而不必推断方向. (3认同)
  • 非常感谢@Tim,在`UIViewController`上声明的` - (UIInterfaceOrientation)interfaceOrientation`方法工作正常. (2认同)

Jim*_*rue 17

正如beno所说,如果您在视图中尽早检测到方向,这似乎是一个更好的答案.我无法获得批准的答案,以便在我的设置中尽早返回结果,但这非常有效.

if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
//DO Portrait
}else{
//DO Landscape
}
Run Code Online (Sandbox Code Playgroud)

  • 自iOS 8.1以来,self.interfaceOrientation已弃用 (2认同)

Shi*_*rin 11

要添加到已经回答的问题中:

您使用[[UIDevice currentDevice] orientation]哪个将产生以下值之一:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;
Run Code Online (Sandbox Code Playgroud)

文档可以在这里找到- (方向)这里 - (UIDeviceOrientation).

(我不是故意要求使用前者,但这些信息对于评论来说非常重要.)


小智 7

试试这个:

[[UIApplication sharedApplication] statusBarOrientation]
Run Code Online (Sandbox Code Playgroud)

或者在Swift 3中:

UIApplication.shared.statusBarOrientation
Run Code Online (Sandbox Code Playgroud)

要专门检查特定方向,您还可以尝试这样的isLandscapeisPortrait属性:

UIApplication.shared.statusBarOrientation.isLandscape
Run Code Online (Sandbox Code Playgroud)

问题[[UIDevice currentDevice] orientation]是,它也将返回UIInterfaceOrientationUnknown哪个statusBarOrientation没有.

还有一个UIViewController属性,interfaceOrientation但在iOS 8中已弃用,因此不推荐使用.

在这里查看文档statusBarOrientation


ben*_*eno 6

如果您遇到困难并且不断从UIDevice获取UIDeviceOrientationUnknown,您还可以使用UIViewController类的interfaceOrientation属性.

有一个很好的总结,为什么[[UIDevice currentdevice]方向]有时会失败:http://bynomial.com/blog/?p = 25,特别是如果你想快速检测方向(例如,如果你想当应用程序退出后台时立即检查).


小智 6

它可以帮助你......

-(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");
 }
}
Run Code Online (Sandbox Code Playgroud)


Dha*_*iya 6

  • 作为 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")
            }
        }
    
    Run Code Online (Sandbox Code Playgroud)