總結(jié)一下關(guān)于navigationbar中各種顏色的修改方法
1、NavigationBar背景色修改
直接修改backgroundcolor屬性達(dá)不到修改的效果县遣,具體原因可自行百度。
修改方法:自定義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文件中調(diào)用
[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;