6

I'm working on an app and I'm not sure how to change the color of the bottom toolbar in swift. I would like to have it as a custom image to match my navigation bar. Does anyone have a code for that or just to change the color. Thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
John Harris
  • 109
  • 1
  • 1
  • 9
  • http://stackoverflow.com/questions/26008536/ios-8-navigationbar-bar-tint-and-title-text-color-swift – Adrian Aug 11 '15 at 02:20

5 Answers5

23

Change background color:

self.toolbar.barTintColor = UIColor.redColor()

Change background image:

self.toolbar.setBackgroundImage(UIImage(named: "BackgroundImage"), forToolbarPosition: .Bottom, barMetrics: .Default)
Bannings
  • 10,376
  • 7
  • 44
  • 54
8

NOTE: All my answers are in Swift 3

In order to change the background color of the toolbar do the following:

self.toolbar.isTranslucent = false
self.toolbar.barTintColor = UIColor.red

This code is similar to @Bannings answer, however his answer is missing the isTranslucent property, which must be set to false first. Otherwise, it won't work.

In order to change the background image do the same as @Bannings suggested:

self.toolbar.setBackgroundImage(UIImage(named: "BackgroundImage"), forToolbarPosition: .bottom, barMetrics: .default)

It should be stated here that the background image is visible due the fact that the isTranslucent property is set to true by default (assuming the background image is not opaque).

It always helps to read the Apple's description on the isTranslucent property for toolbars:

A Boolean value that indicates whether the toolbar is translucent (true) or not (false). The default value is true. If the toolbar has a custom background image, the default is true if any pixel of the image has an alpha value of less than 1.0, and false otherwise. If you set this property to true on a toolbar with an opaque custom background image, the toolbar will apply a system opacity less than 1.0 to the image. If you set this property to false on a toolbar with a translucent custom background image, the toolbar provides an opaque background for the image using black if the toolbar has black style, white if the toolbar has default, or the toolbar’s barTintColor if a custom value is defined.

Andi
  • 8,154
  • 3
  • 30
  • 34
4

If the tool bar is anchored with navigation controller, go to IB to change the color.

1 . go to your navigation controller,

2 . show "document outline"

3 . select "Toolbar" under the navigation controller (usually, it is below Navigation bar)

  1. one the right side, choose your prefered "Bar Tint" / "Translucent"

enter image description here

Vinodh
  • 5,262
  • 4
  • 38
  • 68
2

This also work on swift 3

let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.barTintColor = UIColor.red

Your UIBarStyle should be default.

MiTal Khandhar
  • 275
  • 1
  • 4
  • 15
-2

This also works on swift 3:

UINavigationBar.appearance().barTintColor = UIColor.red
kelvincer
  • 5,929
  • 6
  • 27
  • 32