天天看點

iOS導航欄側滑失效問題iOS導航欄側滑失效問題

iOS導航欄側滑失效問題

關于iOS的導航欄, 想必各個iOS開發者都是經常要面對的問題.也是必須熟練掌握的一個技術點.

比較坑的有兩方面.
  • 1.一方面是導航欄上的控件位置問題.
  • 2.一方面是導航欄的傳回按鈕自定義問題.

今天我主要分享一下自己對這個問題的解決方案的看法.首先我們先來看看iOS中如何設定傳回按鈕.

iOS中設定傳回按鈕有兩種方式.
  • 一種是在上一級控制器配置.(配置backBarButtonItem)
  • 一種是在本控制器配置.(leftBarButtonItem)

前者隻能配置文字或者圖檔.而不能用自定義的View去配置.蘋果的官方文檔有如下解釋

When this navigation item is immediately below the top item in the stack, 
the navigation controller derives the back button for the navigation bar 
from this navigation item. When this property is nil, the navigation item 
uses the value in its title property to create an appropriate back button. If 
you want to specify a custom image or title for the back button, you can 
assign a custom bar button item (with your custom title or image) to this 
property instead. When configuring your bar button item, do not assign a 
custom view to it; the navigation item ignores custom views in the back 
bar button anyway.

當這個屬性是nil的是否, 導航欄使用它的title屬性建立一個傳回按鈕.如果你要為
傳回按鈕自定義一張圖檔或者文字, 你可以指派文字或者圖檔給UIBarButtonItem
對象.但是對于自定義的View, 在backBarButtonItem中會被忽略.
           

後者可以很友善的自定義傳回按鈕.

當同時也存在一個緻命的缺點, 就是用leftBarButtonItem自定義傳回按鈕後, 側滑手勢會失效.如下代碼:

UIView *redView = [UIView new];
redView.width = redView.height = ;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: redView];
self.navigationItem.leftBarButtonItem = item;
           

具體原因:

這是為什麼呢?從iOS7開始, 系統為UINavigationController, 提供了interactivePopGestureRecognizer手勢, 用于右滑傳回(pop).但是如果使用leftBarButtonItem屬性自定義了傳回按鈕, 就會造成手勢失效.要知道具體原因, 我們還要了解, interactivePopGetureRecognizer從手勢觸發到行為發生, 要經曆以下過程.

iOS導航欄側滑失效問題iOS導航欄側滑失效問題

Paste_Image.png

interactivePopGestureRecogizer還存在, 但沒有起作用.是因為delegate裡被阻斷了沒有調用target/action.或者是調用了, 但沒有運作動畫.

如果我們知道action的名字, 則可以添加一個自定義的滑動手勢, 位元組調用系統的action.但是API文檔沒有提供, 是以該action應該是一個私有的API.使用私有API會造成AppStore稽核被拒絕, 是以這個思路也不可取.

那麼, 我們就要自己實作滑動傳回的動畫action, 要麼自己重寫interfactivePopGestureRecognizer手勢的delegate以讓手勢繼續下去, 觸發系統的action裡面對應的動畫.

實作方法:

自定義NavigationController, 遵守手勢協定.

設定interactivePopGestureRecognizer手勢的代理對象為自身(自定義的navigationController自身)

- (void)viewDidLoad { 
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self; 
}
           

讓手勢生效

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
  if (self.viewControllers.count <=  ) { 
      return NO; 
  } 
  return YES; 
}

// 允許同時響應多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer        
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer { 
   return YES;
}
           

禁止響應手勢的是否ViewController中scrollView跟着滾動

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldBeRequiredToFailByGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer {    
return [gestureRecognizer isKindOfClass:
UIScreenEdgePanGestureRecognizer.class];
}
           

在push動畫發生的時候, 禁止滑動手勢, 因為push動作還沒完成, 邏輯上是不允許這個是否進行滑動.重寫pushViewController:XX方法.

self.interactivePopGestureRecognizer.enabled = NO;
           

在使用navigationController的viewController裡面添加

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated];     
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
 }
           

或者使導航控制器成為自身的代理, 監聽Push完成後的ViewController

- (void)navigationController:(UINavigationController *)navigationController       
didShowViewController:(UIViewController *)viewController                    
animated:(BOOL)animate {    
//控制器入棧之後,啟用手勢識别    
  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])        
      self.interactivePopGestureRecognizer.enabled = YES;
}

// 如果要這樣做, 同時應該讓該導航控制器成為自己的代理
// 在上面的viewDidLoad裡添加如下一句代碼
self.delegate = self
           

後續

如果你不想自己實作.這裡有一個南韓開發者利用runtime技術寫的架構, 解決了該問題.https://github.com/devxoul/SwipeBack.

.但是實際測試中, 發現該架構還有不足之處, 如有遇到使用該架構還是無效的, 可以參照上面的步驟進行更改, 自定義導航控制器.關于導航欄上的控件位置問題, 會在下一篇進行介紹.

轉自:http://www.jianshu.com/p/f9f1d1db8e1e