iOS: Device orientation on load iOS: Device orientation on load ios ios

iOS: Device orientation on load


EDIT: I mis-read your question. This will allow you to start your application in certain orientations. Just realized you're trying to figure out the orientation on launch.

There is a method to check the status bar orientation on UIApplication:

[[UIApplication sharedApplication] statusBarOrientation];

Original answer

Try setting the application's accepted device orientations in the plist file:

<key>UISupportedInterfaceOrientations</key><array>    <string>UIInterfaceOrientationPortrait</string>    <string>UIInterfaceOrientationLandscapeLeft</string>    <string>UIInterfaceOrientationLandscapeRight</string></array>

This will indicate that your application supports Portrait (home button at the bottom), landscape left, and landscape right.

Then, in your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {     return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;}

This will tell the UIViewController to auto rotate if the device is in one of your supported orientations. If you wanted to support the upside-down orientation as well (portrait with home button on top) then add that to your plist and just return YES out of this method.

Let us know how it works out.


I think this will work:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

According to the UIDevice reference:
Quote:
"The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications"
I had initially assumed that this property contained the current orientation at all times, but not so, apparently. I guess that turning on notifications is being handled for us behind the scenes in other situations where the orientation property is typically accessed, so it wasn't obvious that this needs to be done manually inside the app delegate


for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.

let orientation = UIApplication.shared.statusBarOrientationif orientation == .portrait {      // portrait   } else if orientation == .landscapeRight || orientation == .landscapeLeft{      // landscape     }

update for depreciation alerts for IOS 13 and Swift 5.x use code block below.

 if #available(iOS 13.0, *) {        let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation        if orientation == .portrait {                     // portrait               } else if orientation == .landscapeRight || orientation == .landscapeLeft{                     // landscape               }    } else {        // Fallback on earlier versions        let orientation = UIApplication.shared.statusBarOrientation        if orientation == .portrait {                            // portrait    } else if orientation == .landscapeRight || orientation == .landscapeLeft{                               // landscape    }    }