自定义导航项未显示在顶级视图控制器上

Custom Navigation Items Not Being Displayed on Top Level View Controller

我有一个项目,我将 TableViewCells 推送到不同的视图控制器。在这个项目中,我使用自定义导航控制器。这是第一个视图控制器中导航控制器的代码:

//Design of Header
        let nav_background = UIImage(named: "header_background")
        navigationController?.navigationBar.setBackgroundImage(nav_background, for: .default)
        navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 1)
        navigationController?.navigationBar.layer.shadowOpacity = 0.16
        navigationController?.navigationBar.layer.shadowRadius = 10

        let account = UIImage(named: "header_account")
        let imageView = UIImageView(image: account)
        let blankView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.layer.shadowOffset = CGSize(width: -3, height: 3)
        imageView.layer.shadowOpacity = 0.16
        imageView.layer.shadowRadius = 10
        self.navigationController?.navigationBar.topItem?.rightBarButtonItem?.customView = imageView
        self.navigationController?.navigationBar.topItem?.leftBarButtonItem?.customView = blankView

第一个视图控制器只显示自定义背景,没有其他属性。

这是在选择 TableView 中的单元格时加载的第二个视图控制器的代码:

//Design of Header
        let account = UIImage(named: "header_account")
        let accountView = UIImageView(image: account)
        let back = UIImage(named: "header_backarrow")
        let backView = UIImageView(image: back)
        navigationItem.leftBarButtonItem?.customView = backView
        navigationItem.rightBarButtonItem?.customView = accountView

理论上,所有内容都应替换为此处编写的代码和视图控制器属性中列出的标题(这适用于除第一个视图控制器之外的所有视图控制器。)我直接从 Apple 网页上获取所有这些内容: https://developer.apple.com/documentation/uikit/uinavigationcontroller

以下是我引用的确切段落:

左项:

If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

中间项:

If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the title property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the title property of the view controller’s navigation item instead.

正确的项目:

If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.

有人知道如何解决这个问题吗?谢谢。

您的代码不起作用,因为在大多数情况下 customViewnil

如果要将导航项设置为 UIImage,请从图像创建一个新的 UIBarButtonItem,然后分配栏按钮项。

例如,来自 UIImage:

let accountItem = UIBarButtonItem(image: account, style: .plain, target: nil, action: nil)
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = accountItem

要从自定义视图创建一个,请使用:

let accountItem = UIBarButtonItem(image: imageView)
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = accountItem

请注意,如果 navigationController 为零或 topItem 为零,这仍然会失败。