UIToolbar setBackgroundColor不会完全改变颜色

Bkd*_*kdD 38 uitoolbar ios7

我正在尝试设置a的背景颜色UIToolBar.我尝试从IB的属性检查器中选择颜色,并尝试以编程方式设置它setBackgroundColor:[UIColor ...].

两种解决方案都有效,但只是部分解决:颜色混合了50%的白色和工具栏非常轻......没有显示我实际选择的颜色,而是更轻的版本.

我怎样才能拥有UIToolBar我选择的实际颜色?解决起来可能很简单,但我找不到办法,也无法在网上找到答案.

Jag*_*een 100

在下面写下代码 viewDidLoad

self.navigationController.toolbar.barTintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

它会将红色设置为工具栏背景.

Reference link https://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 background. 在此输入图像描述


Ash*_*ish 24

在iOS 7中,您需要设置barTintColor属性 -

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

我用它工作正常......


Spe*_*son 5

除了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;
Run Code Online (Sandbox Code Playgroud)