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 = 100;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: redView];
self.navigationItem.leftBarButtonItem = item;
具體原因:
這是為什么呢?從iOS7開始, 系統為UINavigationController, 提供了interactivePopGestureRecognizer手勢, 用于右滑返回(pop).但是如果使用leftBarButtonItem屬性自定義了返回按鈕, 就會造成手勢失效.要知道具體原因, 我們還要了解, interactivePopGetureRecognizer從手勢觸發到行為發生, 要經歷以下過程.
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 <= 1 ) {
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.
.但是實際測試中, 發現該框架還有不足之處, 如有遇到使用該框架還是無效的, 可以參照上面的步驟進行更改, 自定義導航控制器.關于導航欄上的控件位置問題, 會在下一篇進行介紹.