Clambake for iPhone有一個回退按鈕在全部的導航條上.這是一個簡單的沒有文字箭頭.ios
實現一個自定義按鈕是簡單的.相似這個設置controller 的navigationItem一個leftBarButtonItem.app
1 - (void)viewDidLoad 2 { 3 self.navigationItem.leftBarButtonItem = [self backButton]; 4 } 5 6 - (UIBarButtonItem *)backButton 7 { 8 UIImage *image = [UIImage imageNamed:@"back_button"]; 9 CGRect buttonFrame = CGRectMake(0, 0, image.size.width, image.size.height); 10 11 UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame]; 12 [button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 13 [button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal]; 14 15 UIBarButtonItem *item; = [[UIBarButtonItem alloc] initWithCustomView:button]; 16 17 return item; 18 }
可是這樣在iOS7上 pop手勢交互就很差使了.我發現了一個輕鬆解決的辦法.ide
經過個人beta測試者,我收到了不少關於pop手勢的崩潰日誌.post
我發如今棧中推入一個controller後,快速向左平滑,將會引發崩潰.測試
換句話說,若是用戶在推入還在進行的時候當即去點擊返回.那麼導航控制器就秀逗了.spa
我在調試日誌裏面發現這些:調試
nested pop animation can result in corrupted navigation bar
通過幾個小時的奮鬥和嘗試,我發現能夠緩解這個錯誤:日誌
就像Stuart Hall在他的帖子說的那樣,分配了一個手勢交互行爲的委託在自定義按鈕顯示的時候.而後,當用戶快速點擊退出的時候,控制器由於手勢發送了一個消息在自己已經被銷燬的時候.code
個人解決方案是簡單的讓NavigationController本身成爲響應的接受者.最好用一個UINavigationController的子類.orm
1 @interface CBNavigationController : UINavigationController <UIGestureRecognizerDelegate> 2 @end 3 4 @implementation CBNavigationController 5 6 - (void)viewDidLoad 7 { 8 __weak CBNavigationController *weakSelf = self; 9 10 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 11 { 12 self.interactivePopGestureRecognizer.delegate = weakSelf; 13 } 14 } 15 16 @end
在轉場/過渡的時候禁用 interactivePopGestureRecognizer
當用戶在轉場的時候觸發一個後退手勢,則各類事件又湊一塊了.導航棧內又成了混亂的.個人解決辦法是,轉場效果的過程當中禁用手勢識別,當新的視圖控制器加載完成後再啓用.再次建議使用UINavigationController的子類操做.
1 @interface CBNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate> 2 @end 3 4 @implementation CBNavigationController 5 6 - (void)viewDidLoad 7 { 8 __weak CBNavigationController *weakSelf = self; 9 10 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 11 { 12 self.interactivePopGestureRecognizer.delegate = weakSelf; 13 self.delegate = weakSelf; 14 } 15 } 16 17 // Hijack the push method to disable the gesture 18 19 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 20 { 21 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 22 self.interactivePopGestureRecognizer.enabled = NO; 23 24 [super pushViewController:viewController animated:animated]; 25 } 26 27 #pragma mark UINavigationControllerDelegate 28 29 - (void)navigationController:(UINavigationController *)navigationController 30 didShowViewController:(UIViewController *)viewController 31 animated:(BOOL)animate 32 { 33 // Enable the gesture again once the new controller is shown 34 35 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 36 self.interactivePopGestureRecognizer.enabled = YES; 37 } 38 39 40 @end