在自定义的导航控制器中重写push方法,然后修改push后的控制器的返回按钮
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = nil;
}
/**
* 通过拦截push方法来设置每个push进来的控制器的返回按钮
*/
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn setImage:[UIImage imageNamed:@"top_icon_left"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"top_icon_left"] forState:UIControlStateHighlighted];
[btn sizeToFit];
// 让按钮内部的所有内容左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//设置内边距,让按钮靠近屏幕边缘
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(0, 0, 40.0f, 40.0f);
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
viewController.hidesBottomBarWhenPushed = YES; // 隐藏底部的工具条
}
// 一旦调用super的pushViewController方法,就会创建子控制器viewController的view并调用viewController的viewDidLoad方法。可以在viewDidLoad方法中重新设置自己想要的左上角按钮样式
[super pushViewController:viewController animated:animated];
}
-(void)back
{
[self popViewControllerAnimated:YES];
}