UINavigationController全屏滑動(dòng)返回
//注銷系統(tǒng)方法
self.interactivePopGestureRecognizer.enabled = NO;
// 創(chuàng)建全屏滑動(dòng)手勢,調(diào)用系統(tǒng)自帶滑動(dòng)手勢的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
// 設(shè)置手勢代理,攔截手勢觸發(fā)
// 聲明UIGestureRecognizerDelegate 代理
pan.delegate = self;
// 給導(dǎo)航控制器的view添加全屏滑動(dòng)手勢
[self.view addGestureRecognizer:pan];
// 什么時(shí)候調(diào)用:每次觸發(fā)手勢之前都會詢問下代理,是否觸發(fā)。
// 作用:攔截手勢觸發(fā)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 注意:只有非根控制器才有滑動(dòng)返回功能,根控制器沒有。
// 判斷導(dǎo)航控制器是否只有一個(gè)子控制器,如果只有一個(gè)子控制器,肯定是根控制器
if (self.childViewControllers.count == 1) {
// 表示用戶在根控制器界面,就不需要觸發(fā)滑動(dòng)手勢,
return NO;
}
return YES;
}
UINavigationController左邊緣滑動(dòng)返回
// 為了獲取保存系統(tǒng)的 代理
@property (nonatomic ,assign) id target;
// 獲取系統(tǒng)自帶滑動(dòng)手勢的target對象
self.target = self.interactivePopGestureRecognizer.delegate;
// 實(shí)現(xiàn)UINavigationControllerDelegate的代理和聲明
self.delegate = self;
//UINavigationControllerDelegate 代理方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == [self.viewControllers firstObject] )
{
self.interactivePopGestureRecognizer.delegate = self.target;
}
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count>0)
{
// 設(shè)置導(dǎo)航條左邊按鈕的內(nèi)容,把系統(tǒng)的返回按鈕給覆蓋,導(dǎo)航控制器的滑動(dòng)返回功能就消失了
//覆蓋后如果不把系統(tǒng)滑動(dòng)返回功能注銷 或自己重新實(shí)現(xiàn) 在首界面左邊緣滑動(dòng)會崩潰
// 注銷方法self.interactivePopGestureRecognizer.delegate = nil;
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
button.size = button.currentImage.size;
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationBarHidden=NO;
self.interactivePopGestureRecognizer.delegate = nil;
viewController.automaticallyAdjustsScrollViewInsets=NO;
}
[super pushViewController:viewController animated:animated];
}
-(void)backButtonClick
{
[self popViewControllerAnimated:YES];
}