How to get current orientation of device programma

2019-02-05 19:56发布

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:

// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

Precisely, I want to know that what Methods are called on Orientation Changes in iOS.

8条回答
劫难
2楼-- · 2019-02-05 20:29
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.

查看更多
地球回转人心会变
3楼-- · 2019-02-05 20:29

In your ViewController, you can use didRotateFromInterfaceOrientation: method to detect when the device has been rotated and then do whatever you need in every orientation.

Example:

#pragma mark - Rotation

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case 1:
        case 2:
            NSLog(@"portrait");
            // your code for portrait...
            break;

        case 3:
        case 4:
            NSLog(@"landscape");
            // your code for landscape...
            break;
        default:
            NSLog(@"other");
            // your code for face down or face up...
            break;
    }
}
查看更多
疯言疯语
4楼-- · 2019-02-05 20:38

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
}
查看更多
【Aperson】
5楼-- · 2019-02-05 20:38
  @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 

 }
查看更多
甜甜的少女心
6楼-- · 2019-02-05 20:40

You can try this, which solves your problem.

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

Refer below link for App extension Get device current orientation (App Extension)

查看更多
爷的心禁止访问
7楼-- · 2019-02-05 20:43

Follow the documentation on UIDevice.

You need to call

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

查看更多
登录 后发表回答