swift-設(shè)置navigationBar 封裝

功能:1.提供接口,直接調(diào)用添加:左視圖,標(biāo)題,以及右視圖

2.closure(閉包)提供回調(diào)方法,監(jiān)聽點擊事件

3.是不是根視圖:是否添加返回按鈕。

關(guān)鍵代碼實現(xiàn):

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

}

//設(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("類名出錯了", 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!)

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,727評論 1 14
  • //準(zhǔn)備工作 1.刪除Main 2.ARC->MRC(YES->No) 3.刪除文件(ViewConTroller...
    愛吃芒果的淼小豬閱讀 437評論 1 1
  • 1.自定義控件 a.繼承某個控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,698評論 2 4
  • 1.NSString過濾特殊字符串定義一個特殊字符的集合NSCharacterSet set = [NSChara...
    奮拓達(dá)閱讀 925評論 0 0
  • 微涼春風(fēng)扶帷裳,灼灼桃花廢思量。 菩提無根為緣固,鴛鴦?wù)垡戆亚橄琛?紅鸞一鳴姻緣牽,三生石上鳳求凰。 緣起紅塵相思...
    六弦之外閱讀 396評論 0 1

友情鏈接更多精彩內(nèi)容