问题
My iPad application supports all the orientations and it worked fine in iOS 7 as well.
However in iOS 8, launching the application in Landscape mode made my login view draw the landscape view within a portrait frame.
After doing some analysis I found out that the application window does not take the correct orientation while launching in Landscape. Doing a rotation after that corrects the UI since it takes the correct orientation from that point onwards.
Would someone be able to guide me through this? Thanks in advance.
回答1:
The issue seems to be the order of calls when you set up the window. You need to call makeKeyAndVisible
before you assign the rootViewController
. The following works:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.myMainViewController;
But if you change the order to:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = self.myMainViewController;
[self.window makeKeyAndVisible];
You get the behavior you are experiencing.
回答2:
Thank you Dave Kammeyer for the workaround for this annoying iOS8 bug.
If you are working with storyboards you don't really have access to makeKeyAndVisible
because UIApplication
automatically wires everything together before calling AppDelegate
. In this case, I just reset the rootViewController
at the start of application:didFinishLaunchingWithOptions:
UIViewController* rootViewController = self.window.rootViewController;
self.window.rootViewController = nil;
self.window.rootViewController = rootViewController;
Afterwards the app starts in landscape without bugs.
回答3:
I had the root view controller set up in the XIB, by having my navigation controller in the MainWindow.xib, and that navigation ctlr's root view controller the class of my actual root view controller. Thus it would find my root view controller automatically, instantiate it, and hook it op to the navigation controller, and that to the window.
But: apparently that is too early in the game, and causing trouble, when started in landscape mode.
So I broke the Window's connection to the root view controller (being the navigation controller) in the xib, and set the root view controller myself:
self.window.rootViewController = self.navigationController;
Effect is the same as the connection in the nib, but as it happens at a later point it time it is now okay.
回答4:
The following code worked for me
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
application.statusBarOrientation = UIInterfaceOrientationPortrait;
// the rest of the method
}
Hope it will help !!!
来源:https://stackoverflow.com/questions/26211561/ios-8-launching-the-application-in-landscape-mode