ios - 自定义 navigationItem.titleView 未设置

标签 ios swift

我在设置 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 设置为图像的代码。

enter image description here enter image description here

当我运行应用程序时,导航栏的颜色正在更改为正确的颜色,但 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

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

关于ios - 自定义 navigationItem.titleView 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085685/

相关文章:

ios - 如何使用 SwiftUI 获取文本宽度?

iphone - 重构此代码以符合ARC

iphone编程: collision detection of a UIImageView and an animation

swift - GRMustache - 子类上的 MustacheBox

ios - 如何在 Swift 中制作自定义关系运算符

ios - 线高计算错误

iphone - iOS 中带有图片的 XML Gridview

ios - 通过对多关系的计数使用 NSSortDescriptor 获取 CoreData

ios - 可将表格菜单扩展到 Xcode 中的新 View Controller

ios - 应用缩放变换后重新加载 View 会破坏其中的 UIScrollView 框架