功能:1.提供接口,直接調(diào)用添加:左視圖肴熏,標(biāo)題,以及右視圖
2.closure(閉包)提供回調(diào)方法取刃,監(jiān)聽點(diǎn)擊事件
3.是不是根視圖:是否添加返回按鈕出刷。
關(guān)鍵代碼實(shí)現(xiàn):
import UIKit
class YBaseViewController: UIViewController, UIGestureRecognizerDelegate {
//? ? 聲明兩個(gè)左右block
private var leftBarActionBlock: (() -> Void)?
private var rightBarActionBlock: (() -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
//? ? ? ? self.view.backgroundColor = UIColor.whiteColor()
if !isNavRootViewController() {
addBackButton()
}
//設(shè)置導(dǎo)航欄顏色
navigationController?.navigationBar.barTintColor = UIColor.orangeColor()
//設(shè)置中間標(biāo)題字體大小和字體顏色
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName:UIColor.whiteColor()]
}
// MARK: - 導(dǎo)航欄設(shè)置
/**
返回按鈕
*/
func addBackButton() -> Void {
weak var weakSelf = self
addLeftButtonWithImage("nav_back_icon") {
weakSelf!.navigationController?.popViewControllerAnimated(true)
}
}
func addLeftButtonWithImage(image: String, actionClosure:() -> Void) -> Void {
let btn = UIButton.init(type: UIButtonType.Custom)
btn.setImage(UIImage.init(named: image)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forState: UIControlState.Normal)
btn.sizeToFit()
btn.setImage(UIImage.init(named: image + "_heighted"), forState: UIControlState.Highlighted)
navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: btn)
self.leftBarActionBlock = actionClosure
btn.addTarget(self, action: #selector(YBaseViewController.touchLeftBtn), forControlEvents: UIControlEvents.TouchUpInside)
}
@objc private func touchLeftBtn() -> Void {
if self.leftBarActionBlock != nil {
self.leftBarActionBlock!()
}
}
func addRightButtonWithImage(image: String, actionClosure:() -> Void) -> Void {
let btn = UIButton.init(type: UIButtonType.System)
btn.setImage(UIImage.init(named: image)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forState: UIControlState.Normal)
btn.sizeToFit()
btn.setImage(UIImage.init(named: image + "_heighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forState: UIControlState.Highlighted)
navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: btn)
self.rightBarActionBlock = actionClosure
btn.addTarget(self, action: #selector(YBaseViewController.touchRightBtn), forControlEvents: UIControlEvents.TouchUpInside)
}
@objc private func touchRightBtn() -> Void {
if (self.rightBarActionBlock != nil) {
self.rightBarActionBlock!()
}
}
///設(shè)置標(biāo)題navTitle
func setNavTitle(title: String) -> Void {
self.navigationItem.title = title
}
}
extension UIViewController {
//MARK: - 控制器跳轉(zhuǎn)問題PUSH
func pushNextViewController(className: String, value: String, key: String, isHidesBottomBar: Bool) -> Void {
let bundelName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
let classVC: AnyClass? = NSClassFromString(bundelName + "." + className)
print(UIViewController.Type)
if classVC != nil {
let vc = (classVC! as! UIViewController.Type).init()
vc.setValue(value, forKey: key)
vc.hidesBottomBarWhenPushed = isHidesBottomBar
self.navigationController?.pushViewController(vc, animated: true)
}
else {
//YPublicMethod.showTipsWithHUD("類名出錯(cuò)了", showTime: 1.5)
}
}
func isNavRootViewController() -> Bool {
if navigationController == nil {
return false
}
else if(self.navigationController?.viewControllers.count == 1){
return true
}
else {
return false
}
}
/**
*? navigation設(shè)置圖片
*/
func setNavigationImage(image: String?) -> Void {
if image == nil {
navigationController?.navigationBar.translucent = false
return
}
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: image!), forBarMetrics: UIBarMetrics.Default)
navigationController?.navigationBar.shadowImage = UIImage.init(named: image!)
}
}