修改NavigationBar背景色,title颜色,按钮颜色

总结一下关于navigationbar中各种颜色的修改方法

1、NavigationBar背景色修改

直接修改backgroundcolor属性达不到修改的效果,具体原因可自行百度。

修改方法:自定义category

UINavigationBar+BackgroundColor.h

@interface UINavigationBar (BackgroundColor)

- (void)sl_setBackgroundColor:(UIColor*)backgroundColor;

- (void)sl_reset;

@end

UINavigationBar+BackgroundColor.m

#import "UINavigationBar+BackgroundColor.h"
#import <objc/runtime.h>

@implementation UINavigationBar (BackgroundColor)

static char UINavigationBarOverlayKey;

- (UIView *)overlay {
    return objc_getAssociatedObject(self, &UINavigationBarOverlayKey);
}

- (void)setOverlay:(UIView *)overlay {
    objc_setAssociatedObject(self, &UINavigationBarOverlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)sl_setBackgroundColor:(UIColor *)backgroundColor {
    if (!self.overlay) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, UIScreen.mainScreen.bounds.size.width, CGRectGetHeight(self.bounds) + 20)];
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
    }
    self.overlay.backgroundColor = backgroundColor;
}

- (void)sl_reset {
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
    self.overlay = nil;
}

@end

在需要修改navigationBar颜色的.m文件中调用

[self.navigationController.navigationBar sl_setBackgroundColor:[UIColor whiteColor]];

2、修改NavigationBar title颜色

NSDictionary * dict=[NSDictionary dictionaryWithObject:LYWhite forKey:NSForegroundColorAttributeName];
self.navigationController.navigationBar.titleTextAttributes = dict;

3、修改NavigationBar按钮颜色

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 07 APRIL 2015onios,objc,uinavigationbar 这是我们最终想要得到的效果: 思路...
    东引瓯越阅读 4,581评论 1 50
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 7,397评论 2 50
  • 需求生命之力,去扩张力道之本能。需力行于洞见,才可照见世间之美感。其温度所隐藏的极大内力之流,是炙热而滚烫的。如从...
    言色阅读 1,365评论 0 0
  • 心晴YT阅读 1,495评论 0 0