在 iOS 8 之前,我们将以下代码与 supportedInterfaceOrientations 和 shouldAutoRotate 委托(delegate)方法结合使用,以强制应用程序方向为任何特定方向。我使用下面的代码片段以编程方式将应用程序旋转到所需的方向。首先,我正在更改状态栏方向。然后只需呈现并立即关闭模态视图即可将 View 旋转到所需的方向。
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
但这在 iOS 8 中失败了。另外,我在堆栈溢出中看到了一些答案,人们建议我们应该从 iOS 8 开始避免这种方法。
更具体地说,我的应用程序是一种通用类型的应用程序。一共有三个 Controller 。
First View Controller - 它应该支持 iPad 中的所有方向,并且只支持 iPhone 中的纵向(主页按钮向下)。
第二个 View Controller - 它应该在所有条件下只支持横向
第三 View Controller - 它应该在所有条件下都只支持横向
我们正在使用导航 Controller 进行页面导航。从第一个 View Controller ,在按钮单击操作中,我们将第二个压入堆栈。因此,当第二个 View Controller 到达时,无论设备方向如何,应用都应该只锁定横向。
下面是我在第二个和第三个 View Controller 中的 shouldAutorotate
和 supportedInterfaceOrientations
方法。
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
是否有任何解决方案或任何更好的方法来锁定 iOS 8 的特定方向的 View Controller 。请帮助!!
最佳答案
对于 iOS 7 - 10:
objective-C :
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
swift 3:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
只需在呈现的 View Controller 的 -viewDidAppear:
中调用它。
关于ios - 如何在 iOS 8 中强制 View Controller 方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357162/