iosiphoneuitoolbaruicoloruitoolbaritem

Changing the Color of a custom UIToolbar above the keyboard (not just changing the text colour) in iOS 7


Within my app, I am now using the "dark" appearance for the Keyboard for all UITextFields where I was previously using the Light Keyboard.

Above the keyboard for one of the text fields, I have a custom UIToolbar created with some buttons allowing for the user to select one of the options above the keyboard.

This seems a lot harder than it needs to be, but I'm trying to make the UIToolBar dark instead of light and no matter what I try, the toolbar is always white and I can only seem to change the colour of the buttons on the toolbar rather than the toolbar itself.

The toolbar is created in code:

    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;

I've tried it with the commented out code above ( //alertToolBar.tintColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]; ) and the bar always remains white, but it's only the "buttons" that change colour.

How can I change the WHOLE toolbar black?

Any assistance on this would be really appreciated!


Solution

  • I guess you are using iOS 7.0 SDK, so in that case barTint is no longer working for that.

    You would need to use in that case barTintColor.

    Apple's Documentation:

    The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. To tint the bar's background, please use -barTintColor.

    In this case should be:

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