美文网首页
IOS NavigateBar Back Button 全局修改

IOS NavigateBar Back Button 全局修改

作者: Gaooof | 来源:发表于2016-02-24 02:05 被阅读1919次

    IOS全局修改NavigateBar Back Button样式的两种方法:

    1,通过[UIBarButtonItem appearance] 的setBackButtonBackgroundImage方法对back按钮样式自定义 但是此方法中设置forState:UIControlStateHighlighted属性无效 设置图片的imageWithRenderingMode阻止系统渲染也没有用(如有错误还望指正)

    2,通过自定义NavigationController 重写push方法实现对back按钮的全局修改 当需要在局部控制器设置back 按钮样式时 直接可以覆盖 (推荐此方法)

    下面是两种方法的具体实现

    1,

    UIImage*backButtonBackgroundImage = [UIImageimageNamed:@"navigationbar_back"];

    UIImage*backButtonBackgroundImageSelected =[UIImageimageNamed:@"navigationbar_back_highlighted"];

    backButtonBackgroundImage = [backButtonBackgroundImageresizableImageWithCapInsets:UIEdgeInsetsMake(0,backButtonBackgroundImage.size.width-1,0,0)];

    //设置全局属性

    idappearance = [UIBarButtonItem appearance];

    [appearancesetBackButtonBackgroundImage:backButtonBackgroundImage forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];

    [appearance setBackButtonBackgroundImage:backButtonBackgroundImageSelected forState:UIControlStateHighlighted bar Metrics:UIBarMetricsDefault];

    //将文字隐藏

    [appearance setTitleTextAttributes:@{

    NSFontAttributeName:[UIFontsystemFontOfSize:0.1],

    NSForegroundColorAttributeName: [UIColorclearColor]

    }

    forState:UIControlStateNormal];

    2(推荐此方法)

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

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

    @implementation UICustomNavigationController

    -(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated{

    [superpushViewController:viewControlleranimated:animated];

    //NavigationController中包含的第一个UIViewController也是push方法进来的 但是一般这个controller不需要添加back button 

    if(self.viewControllers.count>1) {

    UIButton*backButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

    [backButtonsetBackgroundImage:[UIImageimageNamed:@"navigationbar_back"]forState:UIControlStateNormal];

    [backButtonsetBackgroundImage:[UIImageimageNamed:@"navigationbar_back_highlighted"]forState:UIControlStateHighlighted];

    backButton.frame=CGRectMake(0,0,30,30);

    [backButtonaddTarget:selfaction:@selector(back)forControlEvents:UIControlEventTouchUpInside];

    viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItemalloc]initWithCustomView:backButton];

    }

    }

    //back方法

    -(void)back{

    [self popViewControllerAnimated:YES];

    }

    @end

    最后创建自定义Back 按钮的UICustomNavigationController

    UICustomNavigationController*navVc=[[UICustomNavigationController alloc]  initWithRootViewController:childViewController];

    相关文章

      网友评论

          本文标题:IOS NavigateBar Back Button 全局修改

          本文链接:https://www.haomeiwen.com/subject/svfikttx.html