美文网首页
IOS 改变导航栏颜色

IOS 改变导航栏颜色

作者: 奇梦人 | 来源:发表于2020-11-11 18:16 被阅读0次
    1. 替换系统自带的返回箭头
     UIImage *image = [[UIImage imageNamed:@"leftCancelWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    //箭头的点击事件
    - (void)backClick {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    1. 更换导航栏颜色
      这里需要做兼容,IOS 13以上的系统不支持以前修改颜色的代码

    注意这里改变颜色是全局改变

    UINavigationBar *appearance = [UINavigationBar appearance];
    if (@available(iOS 13.0, *)) {
            
            UINavigationBarAppearance *barAppearance = UINavigationBarAppearance.new;
            barAppearance.backgroundColor = [UIColor colorWithHexString:@"#F4895C"];
            UIBarButtonItemStateAppearance *normal = barAppearance.buttonAppearance.normal;
            if (normal) {
                normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor]};
            }
            
            UIBarButtonItemStateAppearance *highlighted = barAppearance.buttonAppearance.highlighted;
            if (highlighted) {
                highlighted.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]};
            }
            
            appearance.standardAppearance = barAppearance;
            
        } else {
            // 设置文字属性
            NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
            textAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
            // UITextAttributeFont  --> NSFontAttributeName(iOS7)
            textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];            [appearance setTitleTextAttributes:textAttrs];
            
            //设置导航栏的颜色
            [appearance setBarTintColor:[UIColor colorWithHexString:@"#F4895C"]];
            appearance.translucent = YES;
            
        }
    

    相关文章

      网友评论

          本文标题:IOS 改变导航栏颜色

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