gpt4 book ai didi

Swift 4仅旋转UITabBarController的一屏

转载 作者:行者123 更新时间:2023-11-30 11:08:48 26 4
gpt4 key购买 nike

我有一个 UITabBarController 类,并且只想旋转其中一个 ViewController,所有其他 ViewController 应始终以纵向模式显示。

当从 RotateViewController 切换到横向的另一个 ViewController 时,无论旋转如何,屏幕都将保持横向。切换屏幕时,似乎没有“重新评估”正确的旋转。

class TabBarController: UITabBarController {

override var shouldAutorotate: Bool {
if self.selectedViewController is RotateViewController {
return true
}
return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if self.selectedViewController is RotateViewController {
return [.portrait, .landscapeRight, .landscapeLeft]
}
return .portrait
}

强制旋转不起作用。这将以横向方式渲染所有单独的元素,然后旋转整个屏幕。

UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")

最佳答案

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
}

在您的ViewController中添加

override func viewDidLoad {
super.viewDidLoad()

self.canRotate()
}

@objc func canRotate() -> Void {}

关于Swift 4仅旋转UITabBarController的一屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52408601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com