我有 2 个视图控制器,VC1 和 VC2。
VC1 目前以模态方式呈现 VC2。
VC1 仅方向应为纵向,但 VC2 可以具有所有方向。
问题是当我在 VC2 中并且我旋转到横向模式然后关闭时,VC1 也处于横向模式!这绝对不应该发生!
NOTE:没有导航控制器或 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
}
Code in VC2:
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
func canRotate() -> Void {}
链接到我寻求帮助并找到此代码的地方我找到代码的网站 http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift/
非常感谢你的帮助!
您需要按照以下步骤锁定特定 ViewController 的旋转:-
Step 1:
While creating your project, allow all the orientations. Do not select anything in below image.
Step 2:
If you want VC1 to have only Portrait Orientation the implementation then add the below two functions in your ViewController Class
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all //return the value as per the required orientation
}
override var shouldAutorotate: Bool {
return false
}
Step 3:
如果您希望 VC2 具有所有方向,则不要为其添加任何代码。
所以结论是:-
在项目设置中,允许整个项目的所有方向。限制应该在 ViewControllers 级别而不是项目级别。
如果你希望任何 VC 具有全方位的方向,那么就不要编写任何代码。
如果你希望任何 VC 有特定的方向,那么实现上面的两个功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)