问题描述
在iOS 8之前,我们将以下代码与 supportedInterfaceOrientations 和 shouldAutoRotate 委托方法结合使用,以强制将应用定位到任何特定方向。我使用下面的代码片段以编程方式将应用程序旋转到所需的方向。首先,我正在改变状态栏方向。然后只显示并立即关闭模态视图会将视图旋转到所需的方向。
Before iOS 8, we used below code in conjunction with supportedInterfaceOrientations and shouldAutoRotate delegate methods to force app orientation to any particular orientation. I used below code snippet to programmatically rotate the app to desired orientation. Firstly, I am changing the status bar orientation. And then just presenting and immediately dismissing a modal view rotates the view to desired orientation.
[[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开始避免这种方法。
But this is failing in iOS 8. Also, I have seen some answers in stack overflow where people suggested that we should always avoid this approach from iOS 8 onwards.
更具体地说,我的应用程序是一种通用类型的应用程序。总共有三个控制器。
To be more specific, my application is a universal type of application. There are three controllers in total.
-
First View控制器 - 它应该支持iPad中的所有方向iPhone中只有肖像(主页按钮)。
First View controller- It should support all orientations in iPad and only portrait (home button down) in iPhone.
第二个视图控制器 - 它应该只支持所有条件下的横向
Second View controller- It should support only landscape right in all conditions
第三视图控制器 - 它应该只支持所有条件下的横向
Third View controller- It should support only landscape right in all conditions
我们正在使用导航控制器进行页面导航。从第一个视图控制器,在按钮单击操作上,我们在堆栈上推送第二个。因此,当第二个视图控制器到达时,无论设备方向如何,应用程序都应仅锁定横向。
We are using navigation controller for page navigation. From the first view controller, on a button click action, we are pushing the second one on stack. So, when the second view controller arrives, irrespective of device orientation, the app should lock in landscape right only.
以下是我的 shouldAutorotate 第二和第三个视图控制器中的code>和
supportedInterfaceOrientations
方法。
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
是否有任何解决方案可用于锁定视图控制器或更好的方法iOS 8的特定方向。请帮助!!
Is there any solution for this or any better way of locking a view controller in particular orientation for iOS 8. Please help!!
推荐答案
对于iOS 7 - 10:
For iOS 7 - 10:
Objective-C:
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()
只需在显示的视图控制器的 - viewDidAppear:
中调用它。
Just call it in - viewDidAppear:
of the presented view controller.
这篇关于如何在iOS 8中强制查看控制器方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!