简体   繁体   English

仅允许1个视图控制器以横向或纵向显示

[英]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.

相关问题 如何在纵向应用程序中以纵向或横向呈现视图控制器 - How to present a view controller in portrait or landscape, in application that is portrait-only 如何允许特定的视图控制器同时支持横向和纵向 - How to allow a particular view controller to support for both landscape and portrait orientation 为一个视图控制器启用纵向和横向,为swift中的其他视图控制器启用纵向? - Enable portrait and landscape for one view controller and only portrait for other view controllers in swift? 通过仅纵向视图控制器呈现仅横向视图控制器时,状态栏隐藏 - Status bar hiding when landscape-only view controller is presented over portrait-only view controller 如何将某些视图控制器设置为横向和纵向,并且仅限于横向? (IOS) - How to set certain view controller to be landscape and portrait and certain to be landscape only ? (iOS) 仅使用纵向应用在景观上允许视频 - Allow video on landscape with only-portrait app iOS7 / IOS8在视图控制器中仅允许纵向 - IOS7/IOS8 Allow only portrait in view controller iOS:旋转iphone后,仅从支持肖像模式的视图控制器以横向显示视图控制器 - iOS: Presenting a view controller in landscape right from a view controller supporting only portrait mode after rotating the iphone iPhone View Controller横向/纵向旋转的麻烦 - iPhone View Controller landscape / portrait rotation troubles 在仅纵向模式下伪造横向视图 - Fake a landscape view in portrait only mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM