UIWindow的根视图控制器在应用启动时不会旋转为横向 [英] UIWindow's root view controller does not rotate to landscape at app launch

查看:104
本文介绍了UIWindow的根视图控制器在应用启动时不会旋转为横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于xib的仅风景应用程序.该应用程序可以在横向模式下正确启动.但是,我的主ViewController中的视图是纵向显示的.即,将其旋转90度,以使图像显得被裁剪,并且不会占据整个屏幕.如果我使用我的界面来显示模态视图控制器,然后返回到主ViewController,则问题会自行纠正(视图以横向显示).在Xcode 4.2下不会发生此问题.它是在升级到Xcode 4.3之后发生的,并且当我升级项目设置时,唯一进行的代码更改是Xcode自动实现的.

I am developing a xib-based landscape-only app. The app launches in landscape correctly. However, the view in my main ViewController is presented in portrait. That is, it is rotated 90 degrees so that the image appears cropped and does not take up the entire screen. If I use my interface to present a modal view controller then return to the main ViewController, the issue corrects itself (the view is presented in landscape). This problem did not occur under Xcode 4.2. It occurred after upgrading to Xcode 4.3, and the only code changes that were made were automatically implemented by Xcode when I upgraded the project settings.

根据其他文章的建议,我验证了Info.plist的支持的界面方向"和初始界面方向"设置.我为我的每个视图控制器覆盖了shouldAutorotateToInterfaceOrientation方法,以仅对横向显示返回YES.另外,我关闭了视图的自动调整大小,因为我从不希望改变视图的大小/方向.

Based on advice in other posts, I verified my Info.plist settings for Supported Interface Orientations and Initial Interface Orientation. I overrode the shouldAutorotateToInterfaceOrientation method for each of my view controllers to return YES only for landscape orientations. Also, I turned off auto resizing for the view, as I never want the size/orientation of the view to change.

基于此链接[1]中的想法,我怀疑问题在于视图在启动时未收到更改方向的调用,这可能是由于删除了MainWindow.xib概念(该概念似乎已被替换)通过AppDelegate.m中的以下Xcode插入代码:

Based on the ideas in this link [1], I suspected the problem is that the view is not receiving the call to change orientation at launch, possibly due to the removal of the MainWindow.xib concept, which appears to be replaced by the following Xcode-inserted code in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

我修改了此方法,以生成一个通用的根视图控制器,从中显示我的ViewController类,如下面的代码所示:

I modified this method to generate a generic root view controller from which my ViewController class is presented, as shown in the code below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController* myViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.viewController = [[UIViewController alloc] init];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    [self.viewController presentModalViewController:myViewController animated:NO];
    return YES;
}

Voila!这解决了我的问题.但是,对我来说,这感觉像是一个根本性的变化,我现在不想在开发中进行.我打算成为我的根视图控制器的现在是模式视图.有人对此问题有其他解决方案吗?

Voila! This solved my problem. However, to me it feels like a change at a fundamental level, which I don't want to make at this point in my development. What I intended to be my root view controller is now a modal view. Does anyone have another solution to this issue?

提前谢谢!

推荐答案

我遇到了同样的问题:一个应用程序应该是在Landscape中,假定ViewController总是在Portrait中.我对项目和info.plist的各个方面进行了许多更改,包括为主UIWindow提供了一个横向的根视图控制器.它仍然没有用.我最终撤消了所有更改,仅将下面提到的两行添加到我的应用程序委托中:

I had this same issue: an app which was meant to be in Landscape that assumed the ViewController was always in Portrait. I made tons of changes to every aspect of the project and info.plist, including giving the main UIWindow a root-view controller which was landscape. It still didn't work. I eventually undid all the changes and just added the two lines noted below to my app delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
    [_window addSubview:[_viewController view]];
    glView = _viewController.glView;

    // THIS FIXED ORIENTATION FOR ME IN IOS 6
    _window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    // END

    ...my other setup code here...
}

没有其他必要了.

由于某些原因,似乎在iOS 6中,有时在Interface Builder中会忽略UIWindow的root-view-controller设置.我确信我的推理在某处是错误的,但是我认为这很可能有助于使某人朝着更全面,更详尽的答案方向发展.

It appears that, for some reason, in iOS 6 the UIWindow root-view-controller setting is sometimes ignored in Interface Builder. I am sure my reasoning is wrong somewhere, but I thought this might well help prod someone in the direction of a fuller, more exhaustive answer.

这篇关于UIWindow的根视图控制器在应用启动时不会旋转为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆