本文介绍了如何在 iOS 8 中强制视图控制器方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 8 之前,我们将以下代码与 supportedInterfaceOrientationsshouldAutoRotate 委托方法结合使用,以强制应用程序方向为任何特定方向.我使用下面的代码片段以编程方式将应用程序旋转到所需的方向.首先,我正在更改状态栏方向.然后只是呈现并立即关闭模态视图会将视图旋转到所需的方向.

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.

  1. 第一个视图控制器-它应该支持 iPad 中的所有方向,而 iPhone 中仅支持纵向(主页按钮向下).

  1. 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.

下面是我在第二个和第三个视图控制器中的 shouldAutorotatesupportedInterfaceOrientations 方法.

Below is my shouldAutorotate and supportedInterfaceOrientations methods in second and third view controller.

-(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:

目标-C:

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];

斯威夫特 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 中强制视图控制器方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:06