How to customize UINavigationBar for the entire app?

你说的曾经没有我的故事 提交于 2019-12-24 12:30:02

问题


This is a bit different question than the previously asked. So be nice.

  • The entire app has a navigation bar with different colors.
  • The entire app has few options in its navigation bar. They all are same.

What am I expecting?

I have created this extension for UINavigationController and able to change the navigation bar's background color as per the view controller I will be.

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

then, I am trying to add UIBarButton within that extension but they are not showing up. So I need to fix it. And if it will be visible, how do I get its actions call so that I can handle UIBarButton's touch event in the entire app. Properly? No patch, please.

I am using it like this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)       
  self.navigationController?.updateNavigationBar(withViewControllerID: self.restorationIdentifier?)
}

回答1:


Try this and Make extension for 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() 


来源:https://stackoverflow.com/questions/45833384/how-to-customize-uinavigationbar-for-the-entire-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!