我有以下问题。我的应用程序主要处于纵向模式,但对于一些 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/