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

时间:2010-03-25 14:49:56

标签: ios objective-c

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

如果我只写

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

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

有没有办法让iPhone进行当前定位?

13 个答案:

答案 0 :(得分:49)

以下是宏UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait

所以请单独检查,你可以这样做......

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
    // code for landscape orientation      
}

OR

 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
 {
     // code for Portrait orientation       
 }

答案 1 :(得分:33)

按指定here使用[[UIDevice currentDevice] orientation]方法。

答案 2 :(得分:17)

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

if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
//DO Portrait
}else{
//DO Landscape
}

答案 3 :(得分:11)

要添加已经回答的问题:

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

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

可以找到文档here - (orientation)here - (UIDeviceOrientation)

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

答案 4 :(得分:6)

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

有一个很好的总结,为什么[[UIDevice currentdevice]方向]有时会在这里失败:http://bynomial.com/blog/?p=25,特别是如果你想快速检测方向(例如,如果你想在应用程序到来时检查在背景中。)

答案 5 :(得分: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");
 }
}

答案 6 :(得分:6)

Try this:

[[UIApplication sharedApplication] statusBarOrientation]

Or in Swift 3:

UIApplication.shared.statusBarOrientation

To specifically check for a particular orientation you can also try the isLandscape or isPortrait property like so:

UIApplication.shared.statusBarOrientation.isLandscape

The problem with [[UIDevice currentDevice] orientation] is that it will also return UIInterfaceOrientationUnknown which statusBarOrientation does not.

There is also a UIViewController property interfaceOrientation but it was deprecated in iOS 8, so it's not recommended.

You check documentation for statusBarOrientation here

答案 7 :(得分:3)

您还可以定义常数以赚取时间:

#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

答案 8 :(得分:3)

  

  • 作为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")
            }
        }
    

答案 9 :(得分:1)

你可以这样检查(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 :-/
   }
}

如果你在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
    }
}

虽然在这种情况下即使这样就足够了:

private var isPortrait: Bool {
     return self.view.width < self.view.height
}

答案 10 :(得分:1)

将此更新为不推荐使用UIViewController.interfaceOrienation的iOS 8+,您应该使用traitCollections。例如,要在iPhone上检查横向,您可以使用:

if self.traitCollection.verticalSizeClass == .compact
{
  your code
}

请注意,在iPad上并非如此,因为在横向iPad上,尺寸等级不是compactApple Doc Link

Sizes from Apple

答案 11 :(得分:0)

获取当前方向

- (void)viewDidLoad
{
    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"Landscape left");
        self.lblInfo.text = @"Landscape left";
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape right");
        self.lblInfo.text = @"Landscape right";
    } else if (orientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Portrait");
        self.lblInfo.text = @"Portrait";
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@"Upside down");
        self.lblInfo.text = @"Upside down";
    }
}

答案 12 :(得分:0)

如果只是想知道设备是横向还是纵向,那么一个简单的解决方案是(在Swift中):

var orientation = "portrait"
if UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height {
   orientation = "landscape"
}