貢獻做者 -【XJDomain】
博客XJ: https://my.oschina.net/shengbingli/blog
GitHub直播地址: https://github.com/lishengbing/XJDomainLivegit
1:設置導航欄的背景色的三種方法:github
我先說說個人作法:app
XJ-----01dom
> 設置背景色ide
> 去掉那根線字體
> 我採用第二種,使用圖片來設置背景色,這點的好處就是若是你想要更改或者去掉導航欄上的那更顯的話,這種方法搭配一句便可搞定:ui
> 可是使用第三種方法的話,雖然設置背景色很方便,可是去掉下面的一條線的話,這句話沒有效果,不太好設置spa
// 設置背景色 navBar.setBackgroundImage(UIImage(named: navBarBackgroundImageName ?? kBackgroundImageNameType.navBarBg_04BEC6.rawValue), for: .default) navBar.shadowImage = UIImage()
// 經過直接設置bar上的背景色來設置導航欄上的背景色,簡單粗暴,若是不是很計較那根線的話,這樣設置很好 navigationBar.barTintColor = UIColor.orange
XJ-----02.net
> 設置導航欄線條的那根線的顏色的話:切一張圖片 || 本身畫一個圖片也很方便的
> 任意設置frame,寫了一個UIColor的分類---對象方法的獲得圖片,任意修改顏色和顏色的透明度
// 設置背景色 navBar.setBackgroundImage(UIImage(named: navBarBackgroundImageName ?? kBackgroundImageNameType.navBarBg_04BEC6.rawValue), for: .default) navBar.shadowImage = UIColor.red.getImage(frame: CGRect(x: 0, y: 0, width: 1, height: 0.15))
import UIKit extension UIColor { convenience init(r : CGFloat, g : CGFloat, b : CGFloat) { self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1.0) } class func random() -> UIColor { return UIColor(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255))) } func getImage(frame : CGRect) -> UIImage { var image : UIImage! let view = UIView(frame: frame) view.backgroundColor = self UIGraphicsBeginImageContext(view.frame.size) guard let context = UIGraphicsGetCurrentContext() else { return UIImage() } view.layer.render(in: context) image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }
XJ-----03
> 設置導航欄標題的字體大小 、顏色、等等
// 設置UIBarButtonItem上內容的字體大小 let attrs = [NSFontAttributeName : UIFont.systemFont(ofSize: 15), NSForegroundColorAttributeName : UIColor.orange] navigationBar.titleTextAttributes = attrs
XJ-----04
> 設置返回按鈕的字體顏色
// 設置返回按鈕的區域之類的顏色 let navBar = UINavigationBar.appearance(whenContainedInInstancesOf: [CHNavigationController.self]) navBar.tintColor = UIColor.orange
XJ-----05
> 重寫系統返回按鈕:注意我這裏由於項目中沒有用到tabbar,因此不須要判斷第一層之類的,大家懂得!!!
override func pushViewController(_ viewController: UIViewController, animated: Bool) { viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(iamgeNameNormal: "icon_4", iamgeNameHighlighted: "icon_13", target: self, action: #selector(backClick)) super.pushViewController(viewController, animated: animated) } @objc fileprivate func backClick() { popViewController(animated: true) }
XJ-----06
> 你們都知道,只要重寫了系統的返回按鈕的話,就會幹掉系統的側滑手勢,爲了解決這個問題,個人作法是:
> 幹掉系統的側滑手勢,本身自定義一個手勢,可是使用系統的側滑方法便可搞定
func setupPanGes() { guard let systemGes = interactivePopGestureRecognizer else { return } guard let gesView = systemGes.view else { return } let targets = systemGes.value(forKey: "_targets") as? [NSObject] guard let targetObjc = targets?.first else { return } guard let target = targetObjc.value(forKey: "target") else { return } let action = Selector(("handleNavigationTransition:")) //let screenPanGes = UIScreenEdgePanGestureRecognizer() let pan = UIPanGestureRecognizer(target: target, action: action) gesView.addGestureRecognizer(pan) pan.delegate = self self.interactivePopGestureRecognizer?.isEnabled = false }
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { self.view.endEditing(true) if childViewControllers.count == 1 { return false } return true }
XJ-----07
> 需求1: 若是項目中使用導航欄不一樣的背景色的話,我也是採用圖片設置背景色,具體作法以下:
> 外部傳參數這個控制器該顯示什麼背景的導航欄,可是會有默認的導航欄背景色,因此我是重寫了導航欄建立方法的rootVIewController方法
var navBarBackgroundImageName : String? init(rootViewController: UIViewController, navBarBackgroundImageName : String? = kBackgroundImageNameType.navBarBg_04BEC6.rawValue) { super.init(nibName: nil, bundle: nil) self.viewControllers.insert(rootViewController, at: 0) self.navBarBackgroundImageName = navBarBackgroundImageName let navBar = UINavigationBar.appearance(whenContainedInInstancesOf: [CHNavigationController.self]) navBar.setBackgroundImage(UIImage(named: navBarBackgroundImageName!), for: .default) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() setupPanGes() }
extension UIBarButtonItem { convenience init(iamgeNameNormal : String, iamgeNameHighlighted : String, target : Any, action : Any) { let btn = UIButton() btn.setImage(UIImage(named: iamgeNameNormal), for: .normal) btn.setImage(UIImage(named: iamgeNameHighlighted), for: .highlighted) btn.sizeToFit() btn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0) //btn.backgroundColor = UIColor.red btn.addTarget(target, action: action as! Selector, for: .touchUpInside) self.init(customView: btn) } convenience init(title : String, target : Any, action : Any) { let btn = UIButton() btn.setTitle(title, for: .normal) btn.titleLabel?.font = UIFont.systemFont(ofSize: 13) btn.sizeToFit() btn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0) //btn.backgroundColor = UIColor.red btn.addTarget(target, action: action as! Selector, for: .touchUpInside) self.init(customView: btn) } }
XJ-----08
> 注意點:切換導航欄背景色的時候,要想其餘設置都有想過,不能在viewDidLoad()方法中去設置,須要在viewWillAppear(_ animated: Bool)方法中去實現
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) setupUI() }
fileprivate func setupUI() { // 設置返回按鈕的區域之類的顏色 let navBar = UINavigationBar.appearance(whenContainedInInstancesOf: [CHNavigationController.self]) navBar.tintColor = UIColor.orange // 設置背景色 navBar.setBackgroundImage(UIImage(named: navBarBackgroundImageName ?? kBackgroundImageNameType.navBarBg_04BEC6.rawValue), for: .default) navBar.shadowImage = UIImage() // 設置UIBarButtonItem上內容的字體大小 let attrs = [NSFontAttributeName : UIFont.systemFont(ofSize: 15), NSForegroundColorAttributeName : UIColor.orange] navigationBar.titleTextAttributes = attrs }
XJ-----09
> 側滑手勢注意點: 主要是在前一個導航欄和後一個導航欄,一個隱藏了導航欄,一個顯示導航欄,這種切換的側滑手勢效果,要想多級這樣切換效果很好的話,須要注意一個方法的參數:
> 第一級別: animated: false
> 第二級別: animated: animated
> 完美解決亞bug!!!!!
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: false) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(false, animated: false) }
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: animated) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) }
XJ-----010
1:若是第一級別導航欄隱藏了,第二級別顯示了導航欄,這種狀況的話,要想全屏手勢生效,上一個頁面跳轉前具體操做爲:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: animated) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) }
XJ-----011
1:若是第一級別導航欄隱藏了,第二級別繼續隱藏了導航欄,這種狀況的話,要想全屏手勢生效,上一個頁面跳轉前具體操做爲:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: false) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(false, animated: false) }
XJ-----012
1:修改非全局的標題
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.black]