修改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 

@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;

你可能感兴趣的:(修改NavigationBar背景色,title颜色,按钮颜色)