问题
Like many people, I have a splash screen that animates off to reveal the first view of my app. I've been reworking this for the iPad and if you are holding the device in portrait or landscape modes, everything works as intended, the correct default image is used, the correct image that is used to animate this off is used, all orientations work fine.
BUT
If I get the device into landscape mode, and then lay it flat on the table, things go wrong. The correct splash screen is used, but the image used to animate it off is wrong and I have traced this to the following code which is returning 5 - ie none of the portraitupsidedown / portrait / landscapeleft /landscaperight modes that it is meant to return.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
The device itself clearly knows the correct orientation to use as the status bar is correct and indeed the first view controller rotates into the correct orientation - but what is clear is that [UIDevice currentDevice].orientation is not the same as the device actually knows and so the code I am using to deploy appropriate graphics inside the app delegate is wrong.
I guess my question is - how can I fix this? Is there a way of getting the correct device orientation within the app delegate?
回答1:
The UIInterfaceOrientation
is a subset of the UIDeviceOrientation
. If UIDeviceOrientationIsValidInterfaceOrientation
is false, then you can assume usually UIInterfaceOrientationPortrait
. Besides checking the device orientation, you can also check the statusBarOrientation
of UIApplication
, but at startup it may not have a valid value either.
回答2:
Here's a swift solution that worked for me:
if UIDevice.current.orientation.isFlat { //do something }
来源:https://stackoverflow.com/questions/3220989/ipad-launch-orientation-when-flat-issues-in-app-delegate