简体   繁体   English

如何使用UINavigationController(Swift 2.0 iOS 9)强制设置风景方向

[英]How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9)

I tried to find a solution but so much information which doesn't work. 我试图找到一个解决方案,但太多的信息不起作用。 My last try was using the following: 我最后的尝试是使用以下内容:

UIApplication.sharedApplication().setStatusBarOrientation(UIInterfaceOrientation.LandscapeRight, animated: false)

This however, was deprecated from iOS 9 and couldn't find any way to force rotate with UINavigationController . 但是,iOS 9弃用了此方法,找不到使用UINavigationController强制旋转的方法。 My app mainly uses Portrait Orientation and only one view needs to be Landscape. 我的应用程序主要使用纵向方向,并且只有一个视图需要为横向。 I need to force Landscape on one View and rest to keep as Portrait. 我需要在一个视图上强制使用“横向”,然后休息以保持“纵向”。 Any help would be highly appreciated! 任何帮助将不胜感激!

Some of the questions I checked are: 我检查了一些问题:

Setting device orientation in Swift iOS 在Swift iOS中设置设备方向

How do I programmatically set device orientation in iOS7? 如何在iOS7中以编程方式设置设备方向?

Why can't I force landscape orientation when use UINavigationController? 为什么在使用UINavigationController时不能强制横向放置?

If this is something you really want to do, subclass UINavigationController then add this code: 如果这是您真正想要做的事情,则子类UINavigationController然后添加以下代码:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape
}

Trying to force an orientation imperatively is unwise; 试图强制性地定向是不明智的。 it's better to tell iOS what you want (as above) then let it calculate the orientation as best it can. 最好告诉iOS您想要什么(如上所述),然后让它尽可能地计算方向。

We had to do this same thing in our app as well. 我们也必须在我们的应用程序中做同样的事情。 Initially we worked with a hack. 最初,我们曾与黑客合作。 But eventually we switched the "Landscape" VC to a modal rather than part of navigation view controller stack. 但是最终我们将“ Landscape” VC切换为模式视图,而不是导航视图控制器堆栈的一部分。 I would suggest you do that. 我建议你这样做。 But if you really want to, here is how you do it. 但是,如果您真的想要,这里是您的操作方法。

Subclass Navigation VC. 子类导航VC。 in supportedInterfaceOrientaions check for VC type & return appropriate orientation (landscape for one you want, portrait for rest) This itself wont autorotate that VC to landscape, so here is the hack. supportedInterfaceOrientaions检查VC类型并返回适当的方向(对于一个您想要的风景,休息的肖像)这本身不会自动将VC旋转为风景,所以这就是技巧。

In viewDidLoad/viewDidAppear of "landscape" VC, push another generic VC object & pop it subsequently 在“ landscape” VC的viewDidLoad/viewDidAppear中,推送另一个通用VC对象并随后将其弹出

UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

This used to work in iOS7 & we switched to modal after that. 该功能过去曾在iOS7中运行过,此后又切换为模式模式。 so this might now work in later versions. 因此这可能现在可以在更高版本中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM