ios - 如何为某些 View Controller 锁定 iPhone 的方向 - Swift?

标签 ios iphone swift model-view-controller screen-orientation

我有 2 个 View Controller ,VC1 和 VC2。

VC1 目前以模态方式呈现 VC2。

VC1 只有方向应该是纵向,但 VC2 可以有所有方向。

问题是当我在 VC2 中旋转到横向模式然后关闭时,VC1 也处于横向模式!那永远不应该发生!

注意:没有 Navigation Controller 或 UITabbarcontroller

我正在下面添加我的代码。

应用:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
        if (rootViewController.responds(to: Selector(("canRotate")))) {
            // Unlock landscape view orientations for this view controller
            return .allButUpsideDown
        }
    }

    // Only allow portrait (standard behaviour)
    return .portrait;
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    if (rootViewController == nil) { return nil }
    if (rootViewController.isKind(of: (UITabBarController).self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
    } else if (rootViewController.isKind(of:(UINavigationController).self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
    } else if (rootViewController.presentedViewController != nil) {
        return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
    }
    return rootViewController
}

VC2中的代码:

 override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}

func canRotate() -> Void {}

链接到我寻求帮助并找到这段代码的地方 Website where I found Code

非常感谢您的帮助!

最佳答案

您需要按照以下步骤锁定特定 ViewController 的旋转:-

第 1 步: 创建项目时,允许所有方向。不要选择下图中的任何内容。 enter image description here 第 2 步: 如果您希望 VC1 仅实现纵向方向,则在您的 ViewController 类中添加以下两个函数

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.all //return the value as per the required orientation 
    }

override var shouldAutorotate: Bool {
        return false
    }

第 3 步:如果您希望 VC2 具有所有方向,则不要为其添加任何代码。

所以结论:-

在项目设置中,允许整个项目的所有方向。限制应该在 ViewController 级别而不是项目级别。

如果您希望任何 VC 具有所有方向,那么请不要编写任何代码。

如果您希望任何 VC 具有特定的方向,请实现上面的两个功能。

关于ios - 如何为某些 View Controller 锁定 iPhone 的方向 - Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795577/

相关文章:

java - Android (Java) 上 Cocoa 的 NSRange 结构等同于什么?

ios - Flutter 应用程序卡在设备的初始屏幕上

iphone - iPhone应用程序崩溃

ios - 错误域=WebKitErrorDomain 代码=102 "Frame load interrupted"

ios - 解析 Apple Mach O-Linker 错误?

ios - 是否可以使用 iOS 6 实现 3D map View

iphone - 您建议 iPhone 训练营涵盖哪些主题和练习?

swift - 在 Swift 中导入 Sodium 时出现问题

macos - 在应用程序启动时有条件地显示 NSViewController

iOS replaceObjectAtIndex 错误