navigationBar - 基本操作
1.修改基本配置
1.0 设置导航栏背景颜色
self.navigationController?.navigationBar.barTintColor = UIColor.black
1.1 修改导航栏文字颜色
//修改导航栏文字颜色
self.navigationController?.navigationBar.titleTextAttributes =[NSForegroundColorAttributeName: UIColor.white]
1.2 修改导航栏文字字体和大小
//修改导航栏文字字体和大小
self.navigationController?.navigationBar.titleTextAttributes =[NSFontAttributeName: UIFont.italicSystemFont(ofSize: 28)]
1.3 修改导航栏按钮颜色
//修改导航栏按钮颜色
self.navigationController?.navigationBar.tintColor = UIColor.white
1.4 修改导航栏背景图片
self.navigationController?.navigationBar
.setBackgroundImage(UIImage(named: "bg5"), for: .default)
备注:
1.4.1 如果背景图片不需要延伸到状态栏后面,那么背景图片高度是44点(88像素)。
1.4.2 如果需要把导航栏也包含在背景图片下,那么背景图片高度改为64点(128像素)。
2.设置导航栏titleView
2.0 设置导航栏title
self.navigationItem.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor : UIColor.black]
2.1 自定义 - Label
let titleLab = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
titleLab.text = "自定义"
titleLab.textColor = UIColor.orange
self.navigationItem.titleView = titleLab
2.2 自定义 - Image
let imageView = UIImageView(image: UIImage(named : "contentview_imagebg_logo"))
self.navigationItem.titleView = imageView
3.设置导航栏左右按钮
3.0 文字类型
let leftBtn = UIBarButtonItem(title: "leftBtn", style: .plain, target: self, action: #selector(self.leftClick))
let rightBtn = UIBarButtonItem(title: "rightBtn", style: .plain, target: self, action: #selector(self.righClick))
leftBtn.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftBtn
self.navigationItem.rightBarButtonItem = rightBtn
3.1 图片类型
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "fog"), style: .plain, target: self, action: #selector(self.leftClick))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "fog"), style: .plain, target: self, action: #selector(self.rightClick))
3.2 自定义Button
let leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
leftButton.setTitleColor(UIColor.black, for: UIControlState.normal)
rightButton.setTitleColor(UIColor.black, for: UIControlState.normal)
leftButton.setTitle("leftButton", for: UIControlState.normal)
rightButton.setTitle("rightButton", for: UIControlState.normal)
leftButton.addTarget(self, action: #selector(leftClick), for: UIControlEvents.touchUpInside)
rightButton.addTarget(self, action: #selector(rightClick), for: UIControlEvents.touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
事件响应:
@objc private func leftClick() {
print("leftClick")
self.navigationController?.popViewController(animated: true)
}
@objc private func righClick() {
print("rightClick")
}
二 、NavigationBar第三方依赖库
1.导航栏滑动隐藏
HidingNavigationBar:https://github.com/tristanhimmelman/HidingNavigationBar
AMScrollingNavbar:https://github.com/andreamazz/AMScrollingNavba
2.导航栏样式工具
WRNavigationBar:https://github.com/wangrui460/WRNavigationBar_swift\