How to check in which position (landscape or portrait) is the iPhone now?

后端 未结 13 995
旧巷少年郎
旧巷少年郎 2021-01-30 06:47

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

相关标签:
13条回答
  • 2021-01-30 07:38

    You could check it like so (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):

    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
    }
    
    0 讨论(0)
  • 2021-01-30 07:40

    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

    0 讨论(0)
  • 2021-01-30 07:40

    Getting the current orientation

    - (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";
        }
    }
    
    0 讨论(0)
  • 2021-01-30 07:41

    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.

    if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
    //DO Portrait
    }else{
    //DO Landscape
    }
    
    0 讨论(0)
  • 2021-01-30 07:44

    And if you simply want whether the device is in landscape or portrait, a simple solution is (in Swift):

    var orientation = "portrait"
    if UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height {
       orientation = "landscape"
    }
    
    0 讨论(0)
  • 2021-01-30 07:47

    To addon to the already answered question:

    You use [[UIDevice currentDevice] orientation] which will yield one of these values:

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

    The documentation can be found here - (orientation) and here - (UIDeviceOrientation).

    (I don't mean to claim the former anwser, but this information was to big for a comment.)

    0 讨论(0)
提交回复
热议问题