第一步 创建
import UIKit
extension UIBarButtonItem{
//bool 的作用是 判断是否是跟视图 在UINavigationController中判断
convenience init(title: String,target:AnyObject?, action: Selector, isback : Bool = false) {
let btn: UIButton = UIButton(title: title, color: UIColor.red, imageName: "")
if isback{
let imagename = "tabbar_discover"
btn.setImage(UIImage.init(named: imagename), for: .normal)//图片显示
btn.setImage(UIImage.init(named: imagename + "_highlighted"), for: .highlighted)//高亮状态
}else{
}
btn.sizeToFit()//自动适应
btn.addTarget(target, action: action, for: .touchUpInside)
self.init(customView:btn)
}
}
第二步 调用 可在 UINavigationController控制器调用extension WBDiscoverViewController{
func setUPUI() {
navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "自定义", target: self, action: #selector(backclick), isback :true)
}
}
第三步 调用 在主界面中调用UINavigationController控制器
//使用一个字典创建一个子控制器
//return 返回子控制器
fileprivate func addChildViewController(_ vc: UIViewController, title: String, imageName: String) {
vc.title = title
vc.tabBarItem.image = UIImage(named: imageName)
//设置导航图片
let nav = UINavigationController(rootViewController: vc)
addChildViewController(nav)
}