全局修改 NavigateBar 返回图标

**通过自定义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];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • /* UIViewController is a generic controller base class th...
    DanDanC阅读 1,844评论 0 2
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,579评论 2 4
  • 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标...
    VincentHK阅读 5,414评论 3 44
  • 一个app往往有很多界面,而界面之间的跳转也就是对应控制器的跳转,控制器的跳转一般有两种情况 push 或者 mo...
    Dariel阅读 15,459评论 73 186
  • 今天,来宜昌,早晨整理了这段时间的行事历,看到自己一直有在动着。 飞机上看了100页的思考致富,原来金钱,是可以通...
    满眼都是豆豆阅读 257评论 1 2