只做顶级ViewController旋转

时间:2017-09-21 08:04:29

标签: ios swift xcode orientation viewcontroller

我对以下内容有疑问。我的应用主要是在纵向模式,但对于一些视图控制器,我想让它可以旋转到横向模式。该代码适用于topviewcontroller,我想让它可以旋转,但由于它是一种图像弹出视图控制器,你也可以看到后面的控制器也旋转。我不想要那个。我只是想让弹出式视图控制器可以旋转。我该怎么做才能只有topviewcontroller旋转?

我已经在AppDelegate中拥有以下代码:

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
        if (rootViewController.responds(to: Selector(("canRotate")))) {

            return .allButUpsideDown;
        }
    }

    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
}

在我的viewcontrollers文件中:

@IBAction func didDismissButtonPress(sender: UIButton) {
    self.dismiss(animated: true) { () -> Void in
        UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
    }
}

提前感谢您的回答。

1 个答案:

答案 0 :(得分:1)

首先转到目标设置 - >常规标签 - > “部署信息”部分,在设备方向中选择纵向,横向左侧和横向右侧。现在,您可以在pop下面扩展您的viewcontroller,

extension BelowVC {
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }
}

同样,您可以限制任何VC旋转。