修改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...
    肇東周閱讀 12,232評論 4 61
  • 07 APRIL 2015onios,objc,uinavigationbar 這是我們最終想要得到的效果: 思路...
    東引甌越閱讀 1,025評論 1 50
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設置background顏色,可是發現cl...
    以德扶人閱讀 2,461評論 2 50
  • 需求生命之力,去擴張力道之本能。需力行于洞見,才可照見世間之美感。其溫度所隱藏的極大內力之流,是炙熱而滾燙的。如從...
    言色閱讀 200評論 0 0
  • 心晴YT閱讀 212評論 0 0