2014-03-24 48 views
1

在我的应用程序中,我现在使用键盘的“黑暗”外观来显示所有UITextFields,我之前使用Light键盘。在iOS 7中更改键盘上方的自定义UIToolbar的颜色(不仅仅是更改文本颜色)

在其中一个文本字段的键盘上方,我有一个用一些按钮创建的自定义UIToolbar,允许用户选择键盘上方的某个选项。

这似乎比它需要更加困难,但我试图使UIToolBar黑暗,而不是光,无论我尝试什么,工具栏始终是白色的,我似乎只能改变颜色工具栏上的按钮而不是工具栏本身。

工具栏在代码中创建:

UIToolbar *alertToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 
                      self.view.window.frame.size.width, 44.0f)]; 

    alertToolBar.backgroundColor = [UIColor blackColor]; 
    //alertToolBar.tintColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]; 

    alertToolBar.translucent = NO; 
    alertToolBar.items = @[ [[UIBarButtonItem alloc] initWithTitle:@" GBP" 
                   style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(barButtonAddText:)], 
          [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                      target:nil 
                      action:nil], 
          [[UIBarButtonItem alloc] initWithTitle:@" USD" 
                   style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(barButtonAddText:)], 
          [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                      target:nil 
                      action:nil], 
          [[UIBarButtonItem alloc] initWithTitle:@" EUR" 
                   style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(barButtonAddText:)], 
          [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                      target:nil 
                      action:nil], 

    self.itemTextField.inputAccessoryView = alertToolBar; 

我上面(//alertToolBar.tintColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f];)注释掉的代码试了一下,酒吧始终保持白色,但它只是“按钮”改变颜色。

如何更改整个工具栏黑色?

对此的任何援助将非常感谢!

回答

4

我猜你是使用的是iOS 7.0 SDK,所以在这种情况下,barTint不再为工作。

您需要使用barTintColor。

苹果的文档:

tintColor的酒吧的行为已更改在iOS 7.0。它不再影响酒吧的背景,并像添加到UIView中的tintColor属性所描述的那样行为。要着色酒吧的背景,请使用-barTintColor。

在这种情况下应该是:

alertToolBar.barTintColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]; 
+0

非常感谢这一点 - 我不能相信我错过了barTintColor的属性 - 但这个工作像一个绝对的魅力。非常感谢,并为这个令人难以置信的简单问题感到抱歉! – amitsbajaj

2

我想你想设置:

@property(nonatomic, retain) UIColor *barTintColor 
+0

请注意,这是在iOS的7加,所以你可能要检查是否存在属性设置之前。 –

+0

非常感谢大卫。这真的很棒,如果我能接受你和亚历克斯的答案,我会的。我已经投票给你了。非常感谢,因为这确实有帮助。 – amitsbajaj