iOS 14 导航栏标题未显示在以编程方式创建的 UINavigationController 中

标签 ios swift uikit ios14

iOS 14.2,当我尝试使用下面的代码片段以编程方式呈现 NavigationController Controller 时。

@objc private func handleClick() {
    let viewController = MyViewController()
    
    self.present(viewController, animated: true, completion: nil)
}

新 Controller 中的栏标题不会被渲染。我错过了什么吗?

class MyViewController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "TEST" // NOT WORK
        self.navigationItem.title = "Title" // NOT WORK
    }
}

enter image description here

还尝试使用下面的代码片段将常规 View Controller 嵌套到 UINavigableController 中,但标题仍然未呈现。

@objc private func handleHelpClick() {
    let innerVC = MyInnerViewController()
    innerVC.title = "TEST"
    let viewController = UINavigationController(rootViewController: innerVC)
    self.present(viewController, animated: true, completion: nil)
}

最佳答案

文档说:

A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack.

https://developer.apple.com/documentation/uikit/uinavigationcontroller

因此,根据我的理解,您必须为 UIViewController 本身设置标题,而不是为 UINavigationController 设置标题。

示例:

class MyViewController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class ViewController: UIViewController {
    
    private lazy var button: UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitle("Display NavVC", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        btn.addTarget(self, action: #selector(displayNavVC), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        configureButton()
    }
    
    private func configureButton() {
        view.addSubview(button)
        NSLayoutConstraint.activate([
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
    }
    
    @objc
    private func displayNavVC() {
        let vc = UIViewController()
        vc.title = "abc"
        let navigationVC = MyViewController(rootViewController: vc)
        
        self.present(navigationVC, animated: true, completion: nil)
    }
}

结果:

enter image description here

关于iOS 14 导航栏标题未显示在以编程方式创建的 UINavigationController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64411359/

相关文章:

ios - 如何在 iOS 图表上正确显示底轴 (xVals) 上的标签?

iphone - 在 iOS 中从 NSFileWrapper 播放电影

ios - 在 iOS/Node 应用程序中使用 google/twitter/linkedIn 身份验证

ios - 未通过第一个 View Controller 接收 UIImage

ios - if/and 语句中使用 swift for iOS 的奇怪行为

ios - 在 Swift 中以编程方式在两个 subview 之间添加约束

ios - 无法将 iOS 应用上传到应用商店,也无法在应用商店中选择我的 .ipa 文件

ios - UIButton 的子类无法调用 buttonType 初始值设定项

ios - 设备旋转时如何为 View 设置正确的边界?

ios - 添加约束时出错