2012-04-24 70 views
8

有沒有什麼辦法可以將UINavigationController的導航欄的背景設置爲純色?將NavigationBar背景設置爲純色

我知道我可以改變色調的顏色,但仍然留下了漸變/玻璃效果。

任何我可以擺脫的方式,只是有一個普通的舊純色?

回答

5

我認爲你必須繼承UINavigationBar的並覆蓋-(void)drawRect:(CGRect)rect

UIColor *colorFlat = /* any UIColor*/ 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [colorFlat CGColor]); 
CGContextFillRect(context, rect); 
+0

謝謝。奇蹟般有效。要設置按鈕的顏色,我更改了導航欄的色調顏色,並獲得了色調顏色。正如我所料。 – MartinHN 2012-04-24 19:01:34

+0

你知道如果可以通過外觀代理來完成嗎? '[[UINavigationBar appearance] .....];' – MartinHN 2012-04-24 19:04:07

+0

我不知道,我在iOS 4.2的項目中使用此代碼時,外觀不可用 – 2012-04-24 19:07:03

-4

創建CustomUIViewController延伸的UIViewController並覆蓋viewDidLoad中到這樣的事情:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0]; 
} 

之後,用你的CustomUIViewController在控制器中。

Credits

+0

也不是沒有可能的形象呢? – MartinHN 2012-04-24 18:19:43

+0

舒爾,抱歉的錯誤,嘗試使用:[UIColor colorWithRed:0.0綠色:0.0藍色:1.0 alpha:1.0] – kcho0 2012-04-24 18:25:01

+0

沒有運氣。我不應該在導航欄上設置背景嗎?例如'self.navigationController.navigationBar.backgroundColor'?哪個不起作用:) – MartinHN 2012-04-24 18:30:16

46

下面的代碼也導致在導航欄的純色:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]]; 
+2

一定要設置顏色或導航欄上的按鈕不會獲​​得顏色:[[UINavigationBar appearance] setTintColor:[UIColor redColor]]; – 2013-07-16 04:56:07

+0

你在哪裏做這個?我試圖在導航欄的視圖控制器的viewDidLoad中做到這一點,它似乎不起作用。它是否需要提前一點完成。我有一個故事板似乎是壓倒性的。 – David 2015-03-24 14:03:54

+0

大衛,在呈現視圖之前必須完成。配置「外觀」不會更新可見視圖。如果你想在全球範圍內設置,最好的地方是應用程序:didFinishLaunchingWithOptions :. – Lubiluk 2015-03-25 14:38:01

0

我用來覆蓋drawRect方法和填充顏色;但在iOS7升級之後,它會在UINavigationBar上導致一些問題。如果您編寫自己的drawrect方法,即使您調用[super drawRect],它也會更改條​​的尺寸,並最終得到一個44像素高的導航條。狀態欄留空。

爲了得到純色的導航欄,我使用的圖像作爲背景圖片(任何小堅實的彩色圖像會做,因爲你是拉伸它),並添加UINavigationBar的子類的initWithFrame方法這裏面線:

[self setBackgroundColor:[UIColor clearColor]]  
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 
0

下面的代碼爲我工作:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; 
self.navigationController.navigationBar.translucent = NO; 
[self.navigationController.navigationBar setShadowImage:[UIImage new]];