先給大家展示下效果圖:
1.簡介
使用蘋果手機,最喜歡的就是用它的滑動返回.作為一個開發者,我們在編寫很多頁面的時候,總是會因為這樣那樣的原因使得系統的滑動返回不可用.使用導航欄push出一個控制器,我們在控制器中自定義了一個返回按鈕.這樣系統默認的滑動返回手勢效果就沒有了.
2.解決方法
[1]從A這個控制器push到B這個控制器,我們想要自定義B的返回按鈕,我們可以在A中設置
代碼如下復制代碼 self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"王俨"style:UIBarButtonItemStylePlain target:nil action:nil];[2]在B控制器設置返回按鈕,我想這應該是大多數人喜歡的選擇,可是當我們在B控制器中設置self.navigationItem.leftBarButtonItem後發現系統的滑動返回手勢失效了.那麼如何可以保留系統的滑動返回手勢了,我們可以B控制器中這樣做.
代碼如下復制代碼
@interfaceViewController ()
@end@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad]; // 自定義返回按鈕
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0,0,44,44)];
[button setTitle:@"王俨2"forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)back:(UIButton *)button {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated]; if(self.navigationController.viewControllers.count >1) { // 記錄系統返回手勢的代理
_delegate = self.navigationController.interactivePopGestureRecognizer.delegate; // 設置系統返回手勢的代理為當前控制器
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[superviewWillDisappear:animated]; // 設置系統返回手勢的代理為我們剛進入控制器的時候記錄的系統的返回手勢代理
self.navigationController.interactivePopGestureRecognizer.delegate = _delegate;
}#pragma mark - UIGestureRecognizerDelegate- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { returnself.navigationController.childViewControllers.count >1;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { returnself.navigationController.viewControllers.count >1;
}@end