ios - 仅使顶部 ViewController 旋转

标签 ios swift xcode orientation viewcontroller

我有以下问题。我的应用程序主要处于纵向模式,但对于一些 View Controller ,我想让它旋转到横向模式。该代码非常适用于 topviewcontroller,我想使其可旋转,但由于它是一种图像弹出 View Controller ,您还可以看到后面的 Controller 也在旋转。我不想那样。我只想使弹出 View Controller 可旋转。我该怎么做才能只有 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
}

在我的 View Controller 文件中:

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

预先感谢您的回答。

最佳答案

首先转到 Target 的设置 -> 常规选项卡 -> 部署信息部分,在设备方向中选择纵向、横向左侧和横向右侧。现在针对您在 pop 下面扩展 View Controller 的情况,

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

同样,您可以限制任何 VC 的轮换。

关于ios - 仅使顶部 ViewController 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46338617/

相关文章:

ios - swift,为 ios 7 和 8 编写代码

ios - Swift:我无法使自动布局约束适用于我的 UIScrollView

xcode - 如何初始化相互依赖的属性

ios - UIViewController 与键 XXXXX 的键值编码不兼容。 IB 中没有错误的连接

xcode - swift 2 : Function counting occurrence in string not working

ios - 将弱变量分配给强变量时

ios - 改变标签栏文字颜色,iPhone

ios - indexPathForSelectedRow() 的问题,返回不正确的索引。使用 swift

受 IPV6 影响的 iOS 应用内购买非消耗品

ios - Swift IPv6 支持