How to get iPhones current orientation?

后端 未结 5 1496
忘掉有多难
忘掉有多难 2021-01-30 16:19

Is there a special method to get iPhones orientation? I don\'t need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else

相关标签:
5条回答
  • 2021-01-30 17:02

    Here is a snippet of code I hade to write because I was getting wierd issues coming back into my root view when the orientation was changed .... I you see just call out the method that should have been called but did seem to be .... this works great no errors

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
            //do something or rather
            [self 
    shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
            NSLog(@"landscape left");
        }
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
            //do something or rather
            [self 
    shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
            NSLog(@"landscape right");
        }
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
            //do something or rather
            [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
            NSLog(@"portrait");
        }
    }
    
    0 讨论(0)
  • 2021-01-30 17:09

    As discussed in other answers, you need the interfaceOrientation, not the deviceOrientation.

    The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self.interfaceOrientation will do).

    Possible values are:

    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
    

    Remember: Left orientation is entered by turning your device to the right.

    0 讨论(0)
  • 2021-01-30 17:14

    This is most likely what you want:

    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    

    You can then use system macros like:

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
    
    }
    

    If you want the device orientation use:

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    

    This includes enumerations like UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown

    0 讨论(0)
  • 2021-01-30 17:17

    With [UIApplication statusBarOrientation] being deprecated you should now use:

    UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];
    
    UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
    
    0 讨论(0)
  • 2021-01-30 17:20

    UIInterfaceOrientation is now deprecated, and UIDeviceOrientation includes UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown so can't be relied on to give you the interface orientation.

    The solution is pretty simple though

    if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
        // Landscape
    } else {
        // Portrait
    }
    
    0 讨论(0)
提交回复
热议问题