簡體   English   中英

更改導航欄的顏色

[英]change color of navigation bar

如何從默認的藍色更改導航欄的顏色?

謝謝。

假設您已經以編程方式添加了導航欄,而不是在Interface Builder中添加了導航條,只需將其放入viewDidLoad方法中即可。

self.navigationController.navigationBar.tintColor = [UIColor grayColor];

UINavigationBar類具有UIColor *tintColor屬性,您可以在代碼中進行更改。

另外,此屬性也顯示在InterfaceBuilder UI設計工具中。

TintColor屬性不會影響導航欄的默認子視圖(底部邊框和陰影)。 有時,完全覆蓋導航欄的布局很有用。

盡管navigationBar是UINavigationController的只讀屬性,但是您可以通過“ setValue:forKey:”避免此限制。 已成功將5個應用程序提交給AppStore批准了此方法。

您可以繼承UINavigationBar的子類,並根據需要更改drawRect:方法。 例如,

@implementation CustomNavigationBar

- (void) drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
    [backgroundImage drawInRect:rect];
}

之后,可以子類化UINavigationController並更改initWithRootViewController:

- (id) initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithRootViewController:rootViewController]; 
    if (self) 
    {
        CustomNavigationBar *navBar = [CustomNavigationBar new];
        [self setValue:navBar forKey:@"navigationBar"];
    }
    return self;
}

另外,您可以通過為UINavigationController制作Category並為initWithRootViewController實現方法模糊化來改變這種方法:

PS昨天,我的新應用程序出現在AppStore中,這種方法沒有任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM