How to get current orientation of device programmatically in iOS 6?

前端 未结 8 797
庸人自扰
庸人自扰 2020-12-30 23:50

I have developed an iOS App and tested it on iOS6 device. While testing, I realized that my App is not responding to Orientation changes as expected.

Here is my Code

相关标签:
8条回答
  • 2020-12-31 00:16

    This tutorial gives a nice and simple overview of how to handle device rotation using UIDeviceOrientationDidChangeNotification. It should help you to understand how to use UIDeviceOrientationDidChangeNotification to be notified when the device orientation has changed.

    0 讨论(0)
  • 2020-12-31 00:29

    Follow the documentation on UIDevice.

    You need to call

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    

    Then every time the orientation is changed you will receive a UIDeviceOrientationDidChangeNotification.

    0 讨论(0)
  • 2020-12-31 00:31

    You can try this, may help you out:

    How to change the device orientation programmatically in iOS 6

    OR

    Objective-c:

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]
    

    Swift:

    if UIDevice.current.orientation.isLandscape {
        // Landscape mode
    } else {
        // Portrait mode
    }
    
    0 讨论(0)
  • 2020-12-31 00:31
    UIDevice.current.orientation.isLandscape
    

    The accepted answer reads the orientation of the device as above, which can be reported differently than the orientation of your view controller(s), particularly if your device is held in an almost horizontal position.

    To get the orientation of your view controller specifically, you can use its interfaceOrientation property, which is deprecated since iOS 8.0 but still reports correctly.

    0 讨论(0)
  • 2020-12-31 00:35

    Maybe stupid but it is working (only in ViewController):

    if (self.view.frame.size.width > self.view.frame.size.height) {
        NSLog(@"Hello Landscape");
    }
    
    0 讨论(0)
  • 2020-12-31 00:35
      @property (nonatomic) UIDeviceOrientation m_CurrentOrientation ;
    

    /* You need to declare these code in your ViewDidload or ViewWillAppear */

    - (void)viewDidLoad
    
       {
    
       [super viewDidLoad];
    
       [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
       [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
    }
    

    /* Now Our Device will give a notification whenever we change the orientation of our device,So you can control your code or program using the current orientation */

    - (void)deviceOrientationDidChange:(NSNotification *)notification
    
     {
    
     //Obtaining the current device orientation
    
     /* Where self.m_CurrentOrientation is member variable in my class of type UIDeviceOrientation */
    
     self.m_CurrentOrientation = [[UIDevice currentDevice] orientation];
    
        // Do your Code using the current Orienation 
    
     }
    
    0 讨论(0)
提交回复
热议问题