How Do I detect the orientation of the device on iOS?

后端 未结 12 868
小鲜肉
小鲜肉 2020-11-30 01:56

I have a question on how to detect the device orientation on iOS. I don\'t need to receive change notifications, just the current orientation itself. This seems to be a rath

相关标签:
12条回答
  • 2020-11-30 02:34

    Really old thread, but no real solution.

    I Had the same problem, but found out that getting The UIDeviceOrientation isn't always consistent, so instead use this:

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if(orientation == 0) //Default orientation 
        //UI is in Default (Portrait) -- this is really a just a failsafe. 
    else if(orientation == UIInterfaceOrientationPortrait)
        //Do something if the orientation is in Portrait
    else if(orientation == UIInterfaceOrientationLandscapeLeft)
        // Do something if Left
    else if(orientation == UIInterfaceOrientationLandscapeRight)
        //Do something if right
    
    0 讨论(0)
  • 2020-11-30 02:34

    For what You looking for first you have to Get Notification if Orientation Changed! You Can set This Thing in viewDidLoad like

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    and whenever Orientation of your Device changed OrientationDidChange Called where You can do whatever You Want as Per Orientation

    -(void)OrientationDidChange:(NSNotification*)notification
    {
        UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];
    
        if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
        {
        }
        else if(Orientation==UIDeviceOrientationPortrait)
        {
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:36

    if UIViewController:

    if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
    {
        // 
    }
    

    if UIView:

    if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
        //
    }
    

    UIDevice.h:

    #define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
    #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
    

    Updated:

    add this code to xxx-Prefix.pch then you can use it anywhere:

    // check device orientation
    #define dDeviceOrientation [[UIDevice currentDevice] orientation]
    #define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
    #define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
    #define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
    #define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
    

    usage:

    if (isLandscape) { NSLog(@"Landscape"); }
    
    0 讨论(0)
  • 2020-11-30 02:37

    UIViewController has an interfaceOrientation property that you can access to find out the current orientation of a view controller.

    As for your example, that should work. When you say it isn't working, what do you mean? What results does it give you versus what you expected?

    0 讨论(0)
  • 2020-11-30 02:38

    Have you unlocked the hardware lock for device orientation? There is one at the edge of my iPad 1.

    0 讨论(0)
  • 2020-11-30 02:46

    If you want to get device orientation directly from accelerometer use [[UIDevice currentDevice] orientation]. But if you need current orientation of your application(interface orientation) use [[UIApplication sharedApplication] statusBarOrientation].

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