gpt4 book ai didi

swift - 在 swift 4 中为 UINavigationController 和 UIViewController 禁用屏幕方向

转载 作者:可可西里 更新时间:2023-11-01 01:59:19 27 4
gpt4 key购买 nike

您好,我想在我的项目中的一个 View Controller 中停止自转。在下面的快照中, enter image description here

我有一个起始 UINavigationController,然后是一个 UIViewController。我已经实现了以下代码来停止自转:

extension UINavigationController {

override open var shouldAutorotate: Bool {
get {
return false
}
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
return UIInterfaceOrientationMask.landscape
}
}}

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override open var shouldAutorotate: Bool {
return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask
{
return .landscape
}
}

但是上面的代码好像没有调用也没有生效。我正在使用 Xcode 9swift 4.0。任何建议将不胜感激。

问候,妮娜

最佳答案

创建一个类并将其设置为 UINavigationController,然后在您的 Storyboard/XIB 中使用这个新类

在你的扩展中:

class NavigationController: UINavigationController {     
override var shouldAutorotate: Bool {
return topViewController?.shouldAutorotate ?? super.shouldAutorotate
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return topViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
}
}

在你的 Controller 中:

override var shouldAutorotate: Bool {
return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

关于swift - 在 swift 4 中为 UINavigationController 和 UIViewController 禁用屏幕方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47867882/

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