// 1.设置导航栏标题属性:设置标题颜色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
// 2.设置导航栏前景色:设置item指示色
self.navigationController?.navigationBar.tintColor = UIColor.purple
// 3.设置导航栏半透明
self.navigationController?.navigationBar.isTranslucent = true
// 4.设置导航栏背景图片
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)//设置导航栏透明,只有当 self.navigationController?.navigationBar.isTranslucent = true时才有效果
self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "bannerhomeOne"), for: UIBarMetrics.default)//设置有图片背景的导航
// 5.设置导航栏阴影图片----导航栏底部的那一条黑线
self.navigationController?.navigationBar.shadowImage = UIImage()
//6.设置导航栏的背景色----如果设置了背景图片,这个就不起作用了
self.navigationController?.navigationBar.barTintColor=UIColor.init(red:39.0/255, green:131.0/255, blue:207.0/255, alpha:1)
导航上右侧按钮:
func addrightbtn(){
//navitem search 搜索按钮
let buttonSearch = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
buttonSearch.setTitle("设置", for: UIControl.State.normal)
buttonSearch.addTarget(self, action: #selector(settingbtn), for: UIControl.Event.touchUpInside)
let barButtonSearch = UIBarButtonItem(customView: buttonSearch)
//调节按钮位置
let spacer = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
spacer.width = -5;
//设置按钮
self.navigationItem.rightBarButtonItems = [spacer, barButtonSearch]
}
@objc func settingbtn(){
print("设置")
let mysettingvc = LYBMysettingvc.init()
navigationController?.pushViewController(mysettingvc, animated: true)
}
基础导航控制器NAV:
import UIKit
class LYBBaseNav: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
}
func configBackBtn() -> [UIBarButtonItem] {
// 返回按钮
let backButton = UIButton(type: .custom)
// 给按钮设置返回箭头图片
backButton.setImage(UIImage(named: "fanhui"), for: .normal)
// 设置frame
backButton.frame = CGRect(x: 200, y: 13, width: 18, height: 18)
backButton.addTarget(self, action: #selector(back), for: .touchUpInside)
// 自定义导航栏的UIBarButtonItem类型的按钮
let backView = UIBarButtonItem(customView: backButton)
// 重要方法,用来调整自定义返回view距离左边的距离
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
barButtonItem.width = -5
// 返回按钮设置成功
return [barButtonItem, backView]
}
@objc func back(){
self.popViewController(animated: true)
}
//拦截跳转事件,每一次push都会调用
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if self.children.count > 0 {
viewController.hidesBottomBarWhenPushed = true
//添加返回按钮方式一
viewController.navigationItem.leftBarButtonItems=self.configBackBtn()
}
super.pushViewController(viewController, animated: animated)
}
//返回上一级界面之前的设置操作
override func popViewController(animated: Bool) -> UIViewController? {
print("返回");
//每一次对viewController进行push的时候,会把viewController放入一个栈中
//每一次对viewController进行pop的时候,会把viewController从栈中移除
if self.children.count == 2
{
//如果viewController栈中存在的ViewController的个数为两个,再返回上一级界面就是根界面了
//那么要对tabbar进行显示
let controller:UIViewController = self.children[0]
controller.hidesBottomBarWhenPushed = false
}
else
{
//如果viewController栈中存在的ViewController的个数超过两个,对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true
//把tabbar进行隐藏
let count = self.children.count-2
let controller = self.children[count]
controller.hidesBottomBarWhenPushed = true
}
return super.popViewController(animated: false)
}
}
基础控制器VC:
import UIKit
class LYBBaseVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// String.ReachabilitymonitorNet()
// String.ReachabilityOncemonitorNet()
self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
self.view.backgroundColor=UIColor.white
//修改导航栏标题文字颜色
// self.navigationController?.navigationBar.titleTextAttributes =
// [.foregroundColor: UIColor.red,.font:UIFont.systemFont(ofSize: 12)]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//设置导航栏背景透明
//self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
//self.navigationController?.navigationBar.shadowImage = UIImage()
}
//视图将要消失
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//重置导航栏背景
// self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
// self.navigationController?.navigationBar.shadowImage = nil
}
}