swift-设置navigationBar 封装

功能:1.提供接口,直接调用添加:左视图,标题,以及右视图

2.closure(闭包)提供回调方法,监听点击事件

3.是不是根视图:是否添加返回按钮。

关键代码实现:

import UIKit

class YBaseViewController: UIViewController, UIGestureRecognizerDelegate {

//    声明两个左右block

private var leftBarActionBlock: (() -> Void)?

private var rightBarActionBlock: (() -> Void)?

override func viewDidLoad() {

super.viewDidLoad()

//        self.view.backgroundColor = UIColor.whiteColor()

if !isNavRootViewController() {

addBackButton()

}

//设置导航栏颜色

navigationController?.navigationBar.barTintColor = UIColor.orangeColor()

//设置中间标题字体大小和字体颜色

navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName:UIColor.whiteColor()]

}

// MARK: - 导航栏设置

/**

返回按钮

*/

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!()

}

}

///设置标题navTitle

func setNavTitle(title: String) -> Void {

self.navigationItem.title = title

}

}

extension UIViewController {

//MARK: - 控制器跳转问题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("类名出错了", showTime: 1.5)

}

}

func isNavRootViewController() -> Bool {

if navigationController == nil {

return false

}

else if(self.navigationController?.viewControllers.count == 1){

return true

}

else {

return false

}

}

/**

*  navigation设置图片

*/

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!)

}

}

你可能感兴趣的:(swift-设置navigationBar 封装)