[英]Allow only 1 view controller to be in landscape or portrait
my app is portrait what i wanna do is allow 1 view controller to be portrait or landscape i tried using this code in AppDelegate but i am getting an error 我的应用程序是人像,我想做的是允许1个视图控制器为人像或风景,我尝试在AppDelegate中使用此代码,但出现错误
func application(_ application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
let currentViewController: UIViewController? = self.topView
// Get topmost/visible view controller
var currentViewController: UIViewController? = self.topViewController
// Check whether it implements a dummy methods called canRotate
if currentViewController?.responds(to: #selector(self.canRotate)) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
// Only allow portrait (standard behaviour)
return .portrait
}
}
Does anyone know how to do that in swift? 有谁知道如何迅速做到这一点?
I took the following approach in my app. 我在我的应用中采用了以下方法。
Create a custom UINavigationController with the following code & make sure that your root navigation controller is updated to use this as its class. 使用以下代码创建一个自定义UINavigationController,并确保您的根导航控制器已更新为将其用作其类。
import UIKit
class NavigationController: UINavigationController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
if let _ = presentedViewController as? MyPortraitViewController {
return .portrait
}
return .allButUpsideDown
}
}
You can replace MyPortraitViewController
with the class name of the view controller you wish to deviate from the global setting. 您可以将
MyPortraitViewController
替换为要偏离全局设置的视图控制器的类名。
While I use .portrait
in this example, you are free to use other options. 虽然在此示例中使用
.portrait
,但是您可以自由使用其他选项。
One implementation example would be to replace the class of your base navigation controller in your storyboard which is shown below. 一个实现示例是替换情节提要中基本导航控制器的类,如下所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.