iOS全局改变导航栏的返回按钮

要想全局改变导航栏按钮就要自定义NavigationController,重写相应地方法,使用自定义的导航控制器就能实现,代码如下:

.h

#import 

@interface NavController : UINavigationController

@end
.m

#import "NavController.h"

@interface NavController ()

@end

@implementation NavController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.改变导航栏按钮的颜色
    self.navigationBar.tintColor = [UIColor grayColor];
    // 2.滑动返回手势
    self.interactivePopGestureRecognizer.delegate = (id)self;
}

#pragma mark 返回按钮
-(void)popself {
    [self popViewControllerAnimated:YES];
}

#pragma mark 创建返回按钮
-(UIBarButtonItem *)createBackButton {
    return [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"login_navitem"] style:UIBarButtonItemStylePlain target:self action:@selector(popself)];
}

#pragma mark 重写方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    if (viewController.navigationItem.leftBarButtonItem == nil && [self.viewControllers count] > 1) {
        viewController.navigationItem.leftBarButtonItem =[self createBackButton];
    }
}



你可能感兴趣的:(UIBarButtonItem)