我使用“基于窗口的应用程序”模板创建了一个非常基本的 iPad 应用程序。不幸的是,当我旋转模拟器时,提供的 View 不会旋转。
此外,当我将 View Controller 和/或 UINavigationController 添加到基本窗口时,它们也不会旋转。我已经实现了
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
在我添加的 View Controller 中......但仍然看不到任何旋转。
我错过了什么吗?
最佳答案
为了编排自动旋转,您的应用程序的窗口会寻找由 View Controller 管理的最顶层 subview 。窗口在此 View Controller 上调用 shouldAutorotateToInterfaceOrientation:
,并按指示转换 View Controller 的 View 。
这就是为什么基于窗口的应用程序不能立即自动旋转的原因。默认模板不附带已经为您设置的 View Controller 。要利用自动旋转,您必须创建一个自定义的 UIViewController
子类,根据需要编辑其 shouldAutorotateToInterfaceOrientation:
方法,并将该 View Controller 的 View 添加为窗口的直接 subview (或者将 View Controller 添加到像 UINavigationController
这样的容器中,并将该容器 View Controller 的 View 添加为窗口的 subview )。这通常在 application:didFinishLaunchingWithOptions:
中完成。
您声明“当我将 View Controller 和/或 UINavigationController 添加到基本窗口时,它们也不会旋转”,并且您已经实现了 shouldAutorotateToInterfaceOrientation:
,并且它还是不会转。如果您已正确配置所有内容,这应该会起作用。你可能在这个过程中犯了一些小错误,但我不能在没有看到你的项目的情况下确定它是什么。以下是一些需要检查的事项:
如果您在 NIB 中定义了自定义 View Controller ,请确保您记得在 IB 的身份检查器上更改其类名。
确保将 View Controller 的 View 直接添加到窗口,并且没有其他 View 充当窗口和 View Controller 的 View 之间的包装。
<确保您没有尝试向 IB 中的 Window 对象添加任何 subview 。您应该在
application:didFinishLaunchingWithOptions:
中添加它的 subview 。如果您在 NIB 中定义了自定义 View Controller ,请确保其 View 嵌套在 IB 中的 View Controller 下。即不要尝试将其他 View 连接到 View Controller 的
view
导出。
这些是在黑暗中拍摄的一些照片。希望这个答案中的某些内容可以帮助您发现错误。如果没有,请尝试创建一个基于 View 的应用程序并将其默认配置与基于窗口的应用程序进行比较。也许那时你的错误会跳出来。
关于iphone - 基本窗口程序的屏幕旋转不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970765/