最佳答案
在 iOS8之前,我们使用以下代码结合 支持的界面定位和 应该自动旋转委托方法来强制应用程序朝向任何特定的方向。我使用下面的代码片段,以编程方式将应用程序旋转到所需的方向。首先,我要改变状态栏的方向。然后,只要呈现并立即取消一个模态视图,就可以将视图旋转到所需的方向。
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
但是在 iOS8中这是失败的。此外,我还看到了一些关于堆栈溢出的答案,其中有人建议我们应该从 iOS8开始一直避免使用这种方法。
更具体地说,我的应用程序是一种通用类型的应用程序。
第一视图控制器 -它应该支持 iPad 中的所有方向,并且在 iPhone 中只支持肖像(主页按钮向下)。
第二视图控制器 -它应该在所有条件下只支持景观
第三视图控制器 -它应该在所有条件下只支持景观
我们使用导航控制器进行页面导航。从第一个视图控制器,在一个按钮点击动作,我们推动第二个堆栈。因此,当第二个视图控制器到达时,不管设备方向如何,应用程序应该只锁定横向。
下面是我的 shouldAutorotate
和 supportedInterfaceOrientations
方法在第二和第三视图控制器。
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
有没有解决这个问题的方法,或者有没有更好的方法来锁定 iOS8中特定方向的视图控制器。救命啊!