iOS 8 - Launching the application in Landscape mode

杀马特。学长 韩版系。学妹 提交于 2019-12-06 10:08:30

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.

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 rootViewControllerat 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.

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.

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 !!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!