提示
如果想要適配Swift3.0形病,或者Objective-C的自己稍作修改就行。(實在搞不定的可以私聊我...)
正文
iOS的Navigation默認“返回按鈕”就是下面的情況看起來很不爽霞幅。
backbtn.png
我想實現(xiàn)的效果:只有“箭頭”沒有“文字”
Google一番之后漠吻,終于找到了答案!
在自定義的UINavigationController中
1. 重寫下面的方法
func pushViewController(_ viewController: UIViewController,
animated animated: Bool)
2. 實現(xiàn)leftBarButtonItem點擊事件
代碼:
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)
}
一番折騰之后實現(xiàn)了司恳,朋友拿過把玩了一會兒和我說:你這個App怎么沒有側(cè)滑返回功能途乃,你看微信、QQ都有這個功能叭痈怠耍共!
我也覺得側(cè)滑返回功能很不錯,要搞出來的猎塞。之后又各種百度试读、Google,終于搞出來了荠耽,不多說钩骇,上代碼。
override func viewDidLoad() {
super.viewDidLoad()
// 設(shè)置interactivePopGestureRecognizer委托
interactivePopGestureRecognizer?.delegate = self
}
// 自定義back按鈕铝量,制作側(cè)滑返回效果
extension CustomNavigation: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
// 手勢何時有效 : 當(dāng)導(dǎo)航控制器的子控制器個數(shù) > 1就有效
return self.childViewControllers.count > 1
}
}