全局修改 NavigateBar 返回图标

2017-03-21  本文已影响75人  Juno_o
**通过自定义NavigationController 重写push方法实现对back按钮的全局修改,当需要在局部控制器设置back按钮样式时直接可以覆盖 **

首先创建自定义UICustomNavigationController 继承系统的UINavigationController

//重写UICustomNavigationController的:
-(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated 方法:

@implementation HNNavigationController

-(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated {
    
    [super pushViewController:viewController animated:animated];
    
    //NavigationController中包含的第一个UIViewController也是push方法进来的 但是一般这个controller不需要添加back button
    
    if(self.viewControllers.count > 1) {
        
        UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
        
        [backButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"]forState:UIControlStateNormal];
        
        [backButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"]forState:UIControlStateHighlighted];
        
        backButton.frame = CGRectMake(0,0,30,30);
        
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backButton];
        
    }
    
}

//back方法
-(void)back {
    
    [self popViewControllerAnimated:YES];
}

上一篇下一篇

猜你喜欢

热点阅读