UINavigationController控制器push子控制器时,修改返回控件的样式,需要重写系统方法:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
在添加跟控制器的时候也执行该push方法,所以需要进行判断子控制器;
if (self.childViewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:@"backNav"] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@"backNav"] forState:UIControlStateHighlighted];
[backBtn addTarget:self action:@selector(OnClickBack) forControlEvents:UIControlEventTouchUpInside];
[backBtn sizeToFit];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
viewController.navigationItem.leftBarButtonItem = leftItem;
}
[super pushViewController:viewController animated:animated];
可以不用设置backBtn的frame,但是[backBtn sizeToFit];必须写,否则按钮不显示;
OnClickBack执行pop操作:
- (void)OnClickBack{
[self popViewControllerAnimated:YES];
}
这样重写返回按钮,你就会发现原来系统自带的滑动返回手势失效了;所以还需要重新设置一下手势,方法有很多种,我现在只介绍其中最简单方便的一种:
在viewDidLoad中重新设置一些手势的代理:
self.interactivePopGestureRecognizer.delegate = self;
实现一下代理方法:
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return self.childViewControllers.count > 1;
}
这样手势就可以了!!!!搞定~~