我的应用程序主要是肖像,但有一个视图需要横向方向.
我的观点包含在UINavigationController中,显然这是导致此问题的原因.
除了一个之外的所有UIViewControllers都有:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)
需要Landscape的UIViewController有这样的:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)
现在,当用户到达横向UIViewController时会发生什么,它以纵向显示.然后,用户可以旋转他们的手机,并按照我想要的方式显示在风景中(锁定到风景).然后用户继续前进到肖像UIViewController,同样的事情发生:它从横向开始,然后他们旋转他们的手机,它再次成为肖像(并锁定到肖像).
似乎UIViewControllers之间允许方向锁定,但是以某种方式阻止了自动旋转/以编程方式更改方向.
如何强制手机更新到正确的方向?
有一个临时解决方案:我可以检测设备的方向并显示一条消息,要求他们旋转设备,如果它不正确,但这不是最佳的.