提示
如果想要適配Swift3.0,或者Objective-C的自己稍作修改就行。(實(shí)在搞不定的可以私聊我...)
正文
iOS的Navigation默認(rèn)“返回按鈕”就是下面的情況看起來(lái)很不爽。

backbtn.png
我想實(shí)現(xiàn)的效果:只有“箭頭”沒有“文字”
Google一番之后,終于找到了答案!
在自定義的UINavigationController中
1. 重寫下面的方法
func pushViewController(_ viewController: UIViewController,
animated animated: Bool)
2. 實(shí)現(xiàn)leftBarButtonItem點(diǎn)擊事件
代碼:
override func pushViewController(viewController: UIViewController, animated: Bool) {
// 自定義back按鈕
if viewControllers.count != 0 {
viewController.navigationItem.leftBarButtonItem =
UIBarButtonItem(image: UIImage(named: "back.pdf")?.imageWithRenderingMode(.AlwaysOriginal),
style: .Done, target: self, action: Selector.backAction)
// 隱藏tabBar當(dāng)push
viewController.hidesBottomBarWhenPushed = true
}
super.pushViewController(viewController, animated: animated)
}
// back按鈕返回的事件
func back() {
popViewControllerAnimated(true)
}
一番折騰之后實(shí)現(xiàn)了,朋友拿過(guò)把玩了一會(huì)兒和我說(shuō):你這個(gè)App怎么沒有側(cè)滑返回功能,你看微信、QQ都有這個(gè)功能?。?/p>
我也覺得側(cè)滑返回功能很不錯(cuò),要搞出來(lái)的。之后又各種百度、Google,終于搞出來(lái)了,不多說(shuō),上代碼。
override func viewDidLoad() {
super.viewDidLoad()
// 設(shè)置interactivePopGestureRecognizer委托
interactivePopGestureRecognizer?.delegate = self
}
// 自定義back按鈕,制作側(cè)滑返回效果
extension CustomNavigation: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
// 手勢(shì)何時(shí)有效 : 當(dāng)導(dǎo)航控制器的子控制器個(gè)數(shù) > 1就有效
return self.childViewControllers.count > 1
}
}