Swift4.0 - 自定義導(dǎo)航欄

現(xiàn)在為了方便項目的可擴展性我們都習(xí)慣用自定義的控件來覆蓋系統(tǒng)的控件常用的就是自定義導(dǎo)航欄,今天我仿照OC自定義導(dǎo)航欄的方法來用Swift實現(xiàn)了,以供參考:

所有代碼如下:

import UIKit

//遵循手勢代理
class BaseNavigationController: UINavigationController,UINavigationControllerDelegate {

    var popDelegate:UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //navigationBar字體顏色設(shè)置
        self.navigationBar.barTintColor = UIColor.black
        //navigationBar字體顏色設(shè)置
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        
        self.popDelegate = self.interactivePopGestureRecognizer?.delegate
        self.delegate = self
    }
    
    //MARK: - UIGestureRecognizerDelegate代理
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
        //實現(xiàn)滑動返回的功能
        //清空滑動返回手勢的代理就可以實現(xiàn)
        if viewController == self.viewControllers[0] {
            
            self.interactivePopGestureRecognizer?.delegate = self.popDelegate
            
        } else {
            
            self.interactivePopGestureRecognizer?.delegate = nil;
        }
    }
    
    //攔截跳轉(zhuǎn)事件
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.children.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            //添加圖片
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "navigation_left_back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(leftClick))
            //添加文字
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(leftClick))
        }
        super.pushViewController(viewController, animated: animated)
        
    }
    
    //返回上一層控制器
    @objc func leftClick()  {
        
        popViewController(animated: true)
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

注:在設(shè)置返回item只有圖片的時候,有一個坑需要注意,就是如果僅僅直接把圖片是加上去而沒有設(shè)置圖片的渲染類型,這樣效果去藍顏色的顯示的,再初始化圖片的時候需要把圖片的渲染效果.withRenderingMode(.alwaysOriginal)也給加上這樣才是你設(shè)置的圖片的

代碼:

 viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "navigation_left_back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(leftClick))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,201評論 4 61
  • 2018年12月15日星期六晴 這個周六小奧很忙碌,時間被排的滿滿的,上課時間和玩的時間全聚集在了今天,小奧...
    揚帆起航__愛之語閱讀 171評論 0 0
  • 最近一段時間,每天下班后吃晚飯的時候都會追一集《北京女子圖鑒》,這部劇是由戚薇領(lǐng)銜主演的,講述的是一名四川樂山小鎮(zhèn)...
    霞姝兒閱讀 599評論 0 0
  • 悠然自適,方得自然
    雨落隨川閱讀 274評論 0 0
  • 這幾天看《弗洛伊德傳》,終于看到了一個我們所不知道的偉人,弗洛伊德從小就是一個聰明的孩子,讀中學(xué)時,學(xué)習(xí)成績連續(xù)7...
    我是岸閱讀 1,001評論 0 2

友情鏈接更多精彩內(nèi)容