[英]change background color of navigation item (bar)
有一種簡單的方法可以在視圖頂部更改導航項的背景顏色嗎? 我有一個基於導航的應用程序,我只希望一個視圖獲得另一種背景顏色。 我主要用IB創建了視圖。 我找到了以下解決方案(未經測試):
float r=10;
float g=55;
float b=130;
float a=0;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setBackgroundColor:[UIColor colorWithRed:r/255.f
green:g/255.f
blue:b/255.f
alpha:a/255.f];];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
是否有更簡單的方式
[navigationController.navigationBar setBackgroundColor:blueColor]
或者我可以使用IB(但在每個視圖中沒有相同的顏色)嗎?
提前謝謝了!
干杯
你可以使用:
[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example.
這會將導航欄的顏色和所有它的按鈕着色為特定顏色,在本例中為紅色。 也可以在Interface Builder中設置此屬性。
如果您想進一步自定義它,可以通過對其進行子類化將UINavigationBar
的背景設置為圖像。 像這樣......
頭文件。
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomImage)
@end
實現文件。
#import "CustomNavigationBar.h"
@implementation UINavigationBar (CustomImage)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if([self isMemberOfClass: [UINavigationBar class]]){
UIImage *image = [UIImage imageNamed:@"bar.png"];
CGContextClip(ctx);
CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}else{
[super drawLayer:layer inContext:ctx];
}
}
@end
然后在界面生成器設置你的類UINavigationBar
(在這種情況下) CustomNavigationBar
下的身份標簽。
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0 / 256.0 green:145.0 / 256.0 blue:35.0 / 256.0 alpha:1.0]]; 將改變整個應用程序的導航欄的顏色。
只需將其放在Appdelegate的didFinishLauncing方法中即可。
@傑瑞
RGB坐標不支持色調顏色
你可以使用navigationController.navigationBar.tintColor;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.