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

Code Hunter picture Code Hunter · Dec 12, 2012 · Viewed 58.6k times · Source

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.

Answer

Shekhar Gupta picture Shekhar Gupta · Dec 12, 2012

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
}