实现效果
NavigationItem.png目录结构
结构.png主要思路:
--- 首先得有一个UINavigationController 这个在上一篇已经指定好了TabBarItem的根试图是UINavigationController。
如:UINavigationController(rootViewController: TabBarItemVC)
--- 抽一个公共类,用便利构造函数扩充UIBarButtonItem系统类。
--- 设定UINavigationController的外观,比如背景色。
--- 设定UINavigationController的leftBarButtonItem和rightBarButtonItems,比如 图标 addTarget的参数
--- 设定addTarget 触摸事件(Push方法)
1、扩充UIBarButtonItem系统类 用于创建Item(就是左右两边按钮)
extension UIBarButtonItem {
// 在构造函数中 convenience开头 必须明确调用设计的构造函数 self.init()
convenience init(imageName:String, highImageName:String = "", size:CGSize = CGSize.zero, target: Any?, action: Selector){
// 1、创建一个按钮 按钮类型是自定义 .custom
let btn = UIButton(type: .custom)
// 2、设置btn图片和高亮状态下图片
btn.setImage(UIImage(named: imageName), for: UIControlState())
if highImageName != "" {
btn.setImage(UIImage(named: highImageName), for: .highlighted)
}
// 3、设置btn图标的尺寸
// 如果size没传来就用默认值(size:CGSize = CGSize.zero)调用.sizeToFit()方法 系统会根据内容大小自动创建一个大小
if size == CGSize.zero {
btn.sizeToFit()
} else {
// 不然就设置按钮的frame,xy坐标默认(origin: CGPoint.zero),尺寸size用传过来的值
btn.frame = CGRect(origin: CGPoint.zero, size: size)
}
// 4、创建点击事件
btn.addTarget(target, action: action, for: .touchUpInside)
// 5、创建UIBarButtonItem
self.init(customView: btn)
}
2、写一个扩展,用来加载UI,然后把分开写的方法都载入进来。 setupNavigationStyle()、setupNavigationLeftBar()、setupNavigationRightBar()
PS:在系统重写方法载入LoadUI()
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.darkGray
loadUI()
}
}
PS:扩展 HomeViewController 把分开写的方法载入进来
extension HomeViewController {
// 载入所有的UI
func loadUI(){
// 载入 Navigation 样式
setupNavigationStyle()
// 载入 Navigation 左边按钮
setupNavigationLeftBar()
// 载入 Navigation 右边按钮
setupNavigationRightBar()
}
}
PS:设置Navigation外观 setupNavigationStyle()
// navigation 样式外观
func setupNavigationStyle(){
navigationController?.navigationBar.barTintColor = UIColor(r: 215, g: 50, b: 64)
}
PS: 设置左边Item setupNavigationLeftBar()
func setupNavigationLeftBar(){
// 设置图标按钮宽高
let size = CGSize(width: 27, height: 27)
// 左边只有一个图标按钮 用 navigationItem.leftBarButtonItem
navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "crownAnchorBtn", highImageName: "", size: size, target: self, action: #selector(logoItemClick))
}
PS:设置右边Item setupNavigationRightBar()
func setupNavigationRightBar(){
// 设置图标按钮宽高
let size = CGSize(width: 27, height: 27)
// 扫描按钮
let scanBtn = UIBarButtonItem(imageName: "scanBtn", highImageName: "", size: size, target: self, action: #selector(scanBtnItemClick))
// 搜索按钮
let schBtn = UIBarButtonItem(imageName: "schBtn", highImageName: "", size: size, target: self, action: #selector(schBtnItemClick))
// 右边多个图标按钮 用navigationItem.rightBarButtonItems 把所有按钮放进数组里
navigationItem.rightBarButtonItems = [scanBtn,schBtn]
}
PS:扩展 HomeViewController 用来写Click的触摸事件
extension HomeViewController {
// logo点击事件 PUSH到 HomeViewController
func logoItemClick(){
self.navigationController?.pushViewController(HomeViewController(), animated: true)
}
// scanBtn点击事件 PUSH到 scanBtnViewController
func scanBtnItemClick(){
self.navigationController?.pushViewController(scanBtnViewController(), animated: true)
}
//schBtn点击事件 PUSH到 schBtnViewController
func schBtnItemClick(){
self.navigationController?.pushViewController(schBtnViewController(), animated: true)
}
}
网友评论