我正在使用 VIPER 并尝试实现自定义 NavBarController

I'm using VIPER and trying to implement a custom NavBarController

我正在尝试创建自定义导航栏,想在左侧添加标题,在右侧添加聊天图标。

就这样

但它没有显示任何项目。背景颜色是唯一有效的习惯。

我是这样叫的

let garageVC = HomeNavController(rootViewController: GarageRouter.assembleModule(), title: "My Garage")

我的导航class

import UIKit

class HomeNavController: UINavigationController {
    
    var tabTitle: String?
    
    init(rootViewController: UIViewController, title: String) {
        super.init(rootViewController: rootViewController)
        self.tabTitle = title
        configure()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        configure()
    }
    
    private func configure() {
        
        navigationBar.isHidden = false
        navigationBar.isTranslucent = false

        navigationBar.tintColor = .white
        navigationBar.barTintColor = .primary
        
        let nameLabel = UILabel(frame: .zero)
        nameLabel.textColor = .white
        nameLabel.tintColor = .white
        nameLabel.text = tabTitle
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: nameLabel)

        
        let chatButton = UIButton(type: .system)
        chatButton.setImage(UIImage(named: "home-chat")?.withRenderingMode(.alwaysOriginal), for: .normal)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: chatButton)
        chatButton.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: -10)
        chatButton.addTarget(self, action: #selector(chatClicked), for: .touchUpInside)
        
    }
    
    @objc func chatClicked() {
        print("Chat clicked")
    }
}

输出

您可以创建带有标签和按钮的自定义视图,并将其设置为 HomeNavControllernavigationItem 上的 titleView

编辑:

或者您可以将新标签设置为 leftBarButtonItem

如果你想在另一个页面上实现这个导航,你可以只创建自定义视图控制器而不是创建自定义导航控制器。

import UIKit

class CustomViewController: UIViewController {
    
    var tabTitle: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
    }

    private func configure() {
    
        navigationController?.navigationBar.isHidden = false
        navigationController?.navigationBar.isTranslucent = false

        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.barTintColor = .primary
    
        let nameLabel = UILabel(frame: .zero)
        nameLabel.textColor = .white
        nameLabel.tintColor = .white
        nameLabel.text = self.tabTitle
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: nameLabel)

    
        let chatButton = UIButton(type: .system)
        chatButton.setImage(UIImage(named: "home-chat")?.withRenderingMode(.alwaysOriginal), for: .normal)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: chatButton)
        chatButton.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: -10)
        chatButton.addTarget(self, action: #selector(chatClicked), for: .touchUpInside)
    
    }

    @objc func chatClicked() {
         print("Chat clicked")
    }
}

现在,如果您想要更改具有相同导航栏的视图控制器,您可以使用 CustomViewController。

class HomeViewController: CustomViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

如果你想改变标题,你可以在展示或添加到 rootviewcontroller 之前设置视图控制器的标题

let viewController = HomeViewController()
viewController.tabTitle = "Hommme"
let navController = CustomViewController(rootViewController: viewController)
self.present(navController!, animated: true, completion: nil)

如果您使用故事板,您可以在 attibure 检查器上更改视图控制器的标题。

注:

  • 如果要在演示中更改标题,需要调整代码。
  • 如果该导航栏只显示在一页上,您只需直接在视图控制器中进行更改,而不是创建自定义视图控制器。

我对其进行了半硬编码。我已经在我的视图控制器上实现了 configure() 函数并且它可以工作。遗憾的是,我必须为 tabBar.

上的每个视图控制器拖动此函数