2013-10-16 42 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