iOS: Device orientation on load
It seems that when my app loads, it does not know its current orientation:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
Once I rotate the device, I get a correct orientation, but when I load the app, without changing the orientation, it seems that using [[UIDevice currentDevice] orientation]
doesn't know the current orientation.
Is there another way to check this when I first load my app?
Solution 1:
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.
Solution 2:
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
Solution 3:
for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.
let orientation = UIApplication.shared.statusBarOrientation
if 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
}
}
Solution 4:
One thing that nobody has touched on yet is that you’re storing UIDeviceOrientation
types in a UIInterfaceOrientation
variable. They are different, and should not be treated as equal. Note that UIDeviceOrientationLeft
is equal to UIInterfaceOrientationRight
(since the interface rotates the opposite way compared to the device).