如何为整个应用自定义 UINavigationBar? [英] How to customize UINavigationBar for the entire app?

查看:26
本文介绍了如何为整个应用自定义 UINavigationBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与之前提出的问题有点不同.对我好点.

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

  • 整个应用都有一个不同颜色的导航栏.
  • 整个应用的导航栏中几乎没有选项.他们都是一样的.

我在期待什么?

我为 UINavigationController 创建了这个扩展,并且能够根据我将要使用的视图控制器更改导航栏的背景颜色.

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

然后,我尝试在该扩展中添加 UIBarButton 但它们没有显示.所以我需要修复它.如果它是可见的,我如何获得它的动作调用,以便我可以在整个应用程序中处理 UIBarButton 的触摸事件.正确?没有补丁,请.

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.

我是这样使用它的:

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

推荐答案

试试这个并为 UINavigationItem 制作扩展.

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() 

这篇关于如何为整个应用自定义 UINavigationBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆