这个问题与之前提出的问题有所不同。那就好

  • 整个应用程序均具有不同颜色的导航栏。
  • 整个应用程序的导航栏中没有几个选项。他们都是一样的。

  • 我期望吗?

    我已经为UINavigationController创建了此扩展名,并且能够根据将要使用的视图控制器来更改导航栏的背景色。
    extension UINavigationController {
    
        func updateNavigationBar(withViewControllerID identifier: String?) {
    
            setupNavigationBarButtons()
    
            if identifier == kFirstVCIdentifier {
                self.navigationBar.tintColor = .white
                self.navigationBar.barTintColor = .red
            } else if identifier == kSecondVCIdentifier {
                self.navigationBar.tintColor = .white
                self.navigationBar.barTintColor = .green
            } else if identifier == kThirdVCIdentifier {
                self.navigationBar.tintColor = .white
                self.navigationBar.barTintColor = .blue
            }
    
            self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    
            self.navigationBar.isTranslucent = false
            self.navigationBar.clipsToBounds = false
            self.navigationBar.shadowImage = UIImage.init()
            self.view.backgroundColor = .clear
        }
    
        internal func setupNavigationBarButtons() {
            let barButtonItemSettings = UIBarButtonItem.init(title: "Settings", style: .plain, target: self, action: #selector(actionSettings))
            let barButtonItemSpace = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let barButtonItemLogOut = UIBarButtonItem.init(title: "LogOut", style: .plain, target: self, action: #selector(actionLogOut))
            let buttons = [barButtonItemSettings, barButtonItemSpace, barButtonItemLogOut]
            self.navigationItem.rightBarButtonItems = buttons
        }
    
        @objc internal func actionSettings() {
            print("Settings")
        }
    
        @objc internal func actionLogOut() {
            print("LogOut")
        }
    }
    

    然后,我试图在该扩展名中添加UIBarButton,但它们没有显示出来。所以我需要修复它。如果它是可见的,我该如何调用它的action以便我可以在整个应用程序中处理UIBarButton的touch事件。 正确吗?请没有补丁。

    我正在这样使用它:
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
      self.navigationController?.updateNavigationBar(withViewControllerID: self.restorationIdentifier?)
    }
    

    最佳答案

    尝试此操作,并为UINavigationItem扩展。

    extension UINaviationItem {
    
    func setupNavigationBarButtons()
    {
        let barButtonItemSettings = UIBarButtonItem.init(title: "Settings", style: .plain, target: self, action: #selector(actionSettings))
        let barButtonItemSpace = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let barButtonItemLogOut = UIBarButtonItem.init(title: "LogOut", style: .plain, target: self, action: #selector(actionLogOut))
        let buttons = [barButtonItemSettings, barButtonItemSpace, barButtonItemLogOut]
        self.rightBarButtonItems = buttons
    }
    
    @objc  func actionSettings() {
    
        print("Settings")
    
      if let current = UIApplication.shared.delegate?.window
        {
            var viewcontroller = current!.rootViewController
            if(viewcontroller is UINavigationController){
                viewcontroller = (viewcontroller as! UINavigationController).visibleViewController
    
                print( "currentviewcontroller is %@/",viewcontroller )
        }
        }
    }
    @objc  func actionLogOut() {
        print("LogOut")
    }
    
    }
    
    self.navigationController?.updateNavigationBar(withViewControllerID: "second")
    self.navigationItem.setupNavigationBarButtons()
    

    09-05 20:21