2013-10-16 47 views
32

我試圖設置UIToolBar的背景顏色。 我嘗試從IB的Attribute Inspector中選擇顏色,並嘗試通過setBackgroundColor:[UIColor ...]編程設置它。UIToolbar setBackgroundColor不能完全改變顏色

這兩種解決方案都可以工作,但只是部分:顏色與白色混合50%,而工具欄非常輕巧......不會顯示我實際選擇的顏色,而是它的一個更輕的版本。

我該如何選擇我選擇的實際顏色UIToolBar? 解決這個問題可能非常簡單,但我找不到方法,也無法在網上找到答案。

回答

85

下面的代碼寫在你的viewDidLoad

self.navigationController.toolbar.barTintColor = [UIColor redColor]; 

它將爲紅色作爲您的工具欄背景。

Reference linkhttps://web.archive.org/web/20160321155823/https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW5

在這裏面,他們說,Use barTintColor to tint the bar backgroundenter image description here

+0

啊是的,我只是嘗試和工作!不能相信我以前搜索時沒有看到這個。非常感謝。 – BkdD

+1

對不起,進一步的問題...我現在嘗試使用更具體的顏色,使用UIColor colorWithRed構建它:green:blue:alpha:但顏色再次顯示不正確..這次基本保持相同的色調但下降飽和度和亮度約25%,這裏是一個圖片:[圖片](https://docs.google.com/file/d/0B4pmO_xiSW_4SjQ1N0xBeGpFUG8/edit?usp=sharing) 在圖片你看到左邊的顏色是我想要的顏色,正確的顏色是它實際上變成的顏色。你有什麼想法我做錯了嗎? – BkdD

+0

對不起,對於遲到的回覆,我很忙,我沒有任何想法,但我會在免費時檢查它。如果你有解決方案,讓我知道k? – Jageen

21

在iOS中7,你需要設置barTintColor屬性 -

UIToolbar *doneToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 584, 320, 44)]; 
doneToolbar.translucent=NO; 
doneToolbar.barTintColor=[UIColor redColor]; 
[self.view addSubview:doneToolbar]; 

我已經用它的做工精細...

0

UIToolbar * numberToolbar = [[UIToolbar頁頭] initWithFrame :CGRectMake(0,0,320,50)];

numberToolbar.backgroundcolor = [UIColor redcolor]; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc] initWithTitle:@「Clear」style:UIBarButtonItemStyleBordered
nil];

[numberToolbar sizeToFit]; 
numberTextField.inputAccessoryView = numberToolbar; 
0

在整個應用程序:

UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR 

if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) { 
     UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal) 

    } 
0

試試這個IOS上10:

let dummyToolbar = UIToolbar() 
dummyToolbar.barTintColor = .lightGray 
dummyToolbar.sizeToFit() // without this line it doesn't work 
3

除了Jageen的回答,您還必須將半透明的屬性設置爲false。否則,顏色將比barTintColor指定的顏色具有稍低的飽和度和色調。

// Sets to a specific color 
self.navigationController.toolbar.barTintColor = UIColor colorWithRed:6.0/255.0 green:52.0/255.0 blue:90.0/255.0 alpha:1.0]; 

// Without this, color will be faded slightly and not exactly what's specified above 
self.navigationController.toolbar.translucent = false; 
+0

感謝提醒它;)「半透明=假」 – Evsenev