【问题标题】:I'm using VIPER and trying to implement a custom NavBarController我正在使用 VIPER 并尝试实现自定义 NavBarController
【发布时间】:2021-04-05 03:16:45
【问题描述】:

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

就这样

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

我这样称呼它

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

我的导航课

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")
    }
}

输出

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar viper-architecture


    【解决方案1】:

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

    Check out this answer

    编辑:

    您也可以将新标签设置为leftBarButtonItem

    How do I left align the title of a navigation bar in Xcode?

    【讨论】:

    • 我尝试了两个答案,但遗憾的是没有任何改变
    • 你确定你设置了正确的tabTitle并且值不是nil吗?
    【解决方案2】:

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

    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)
    

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

    注意:

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

    【讨论】:

    • 感谢您的努力。您的回答似乎可行,但遗憾的是我在我的 VC 中使用 BaseController,我不想在其上实现导航栏。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多