自定义 navigationItem.titleView 未设置

Custom navigationItem.titleView not setting

我在设置 self.navigationItem.titleView 时遇到了问题,谁能帮我纠正错误。

import Foundation

class CustomNavigationController: UINavigationController
{
    override func viewDidLoad() {
        let logo = UIImage(named: "browse_back")
        var hexColor = 0x21BBD4 as UInt
        self.navigationBar.barTintColor = GeneralHelper.UIColorFromRGB(hexColor)
        self.navigationItem.titleView = UIImageView(image: logo)
    }
}

这是我将 titleView 设置为图像的代码。

当我 运行 应用程序时,导航栏的颜色正在更改为正确的颜色,但 titleView 图像未显示。

谢谢。

The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content.

来源:UINavigationItem Class Reference

您必须设置控制器 navigationItemtitleView,该控制器是自定义导航控制器管理的导航堆栈中最顶层的控制器。

对于那些使用 UILabel 作为 titleView 的人

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    navigationItem.titleView?.sizeToFit()
}

希望这有效!