1. 程式人生 > >IOS 開發筆記——自定義導航欄返回鍵後如何恢復系統預設返回手勢

IOS 開發筆記——自定義導航欄返回鍵後如何恢復系統預設返回手勢

       在開發當中,我們不得不要將導航欄的左邊返回按鈕自定義成不同的樣式,但是一當我們自定義樣式後,系統預設的 pop 手勢就失效了,那麼我們該如何做呢?其實這個問題網上也有很多答案,但是每一個答案都非常坑,我都不得不吐槽一下了,老是將問題複雜化,不懂裝懂,最討厭這些人了,好了,我們繼續開始:

        1.自定義一個導航欄,並重寫它的 ViewDidLoa()方法

class CMNavgationController: UINavigationController, UIGestureRecognizerDelegate {

  override func viewDidLoad() {
        unowned let weakSelf =  self
        if self.respondsToSelector("interactivePopGestureRecognizer"){
            self.interactivePopGestureRecognizer.delegate =  weakSelf
            self.delegate = weakSelf
  }

<span style="font-size:14px;"><strong></strong></span>}

當導航欄一向應 "interactivePopGestureRecognizer" 這個預設響應的一個手勢,我們就重新設定手勢的代理不再是系統的 UINavagtionController,而是我們的代理 "CMNavgationController" 這個導航控制器 , 而且這個導航控制器的代理也必須為它自己,因為這個導航控制器等一下還要監聽它自己的 navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) 這個代理方法

-----------------------------------------分割線-----------------------------------

      2.重寫導航欄的 pushViewController 方法

 override func pushViewController(viewController: UIViewController, animated: Bool) {

    if viewControllers.count > 0 {

          if self.respondsToSelector("interactivePopGestureRecognizer"){
                self.interactivePopGestureRecognizer.enabled = false;
            }

   }
  super.pushViewController(viewController, animated: true)
}
重寫 push 方法最主要的目的是讓這個 pop 手勢在 push 的時候變為不可用,因為可能在 push 過程中使用者很快的進行了左滑動,那麼就會造成 pop 棧中的混亂,導致畫面卡頓,所以為了避免這個情況,我們必須要設定在 Push 過程中禁止這個手勢的響應

-----------------------------------------分割線-----------------------------------

   3.設定導航欄代理方法,監聽導航欄是否 顯示完畢

extension CMNavgationController: UINavigationControllerDelegate {

  func navigationController(navigationController: UINavigationController, didShowViewController
       viewController: UIViewController, animated: Bool) {

        if (self.viewControllers.count == 1) {
            self.interactivePopGestureRecognizer.enabled = false;
        }else{
            self.interactivePopGestureRecognizer.enabled = true;
        }
    }
}
這個方法主要是監聽導航欄是否顯示完畢,當顯示完畢的時候我們要進行一個判斷,因為當控制器ViewController 數量為1的時候,使用者如果進行了右滑,但問題是當前控制器數量只有一個,那個這個方法 POP 什麼呢?根本就沒法 POP 嘛,也會造成了 pop 棧中的混亂,導致畫面一直卡頓,所以我們也要進行一個判斷,判斷控制器數量只有一個的時候,我們就禁止了這個手勢,反之則開啟

-----------------------------------------分割線-----------------------------------

  4。在普通的控制器中進行自定義左邊的返回按鈕

class HomeViewController:UIViewController {        
    override func viewDidLoad() {
        super.viewDidLoad()
        var button = UIButton(frame: CGRectMake(0, 0, 80, 40))
        button.setImage(UIImage(named: image), forState: UIControlState.Normal)
        button.addTarget(self, action: "back", forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
    }

   func back(){
        self.popViewControllerAnimated(true)
    }
}
好了,到這裡就一齊都結束了,完美解決了這個手勢,但是如果需要更漂亮的手勢的話就要自己自定義 POP 手勢了,而 蘋果公司也給我們提供了這些方法,在這裡我留著下次再說了,寫這篇文章完全為了平坑,網上的人亂寫一通,不想更多的人去踩坑了。

talk is chep,show me the code