13

I have an app that's been around since iOS 8. the way it's working is that you have a UITableView and tap on cells to produce a score, depending on the score the UItoolbar at the bottom changes color using this method :

func updateToolbarAndLabel(score: Int) {

    if(score <= -1) {
        self.toolbar.backgroundColor = UIColor.greenColor()
    } else if(score <= 0) {
        self.toolbar.backgroundColor = .None
    } else if(score <= 9) {
        self.toolbar.backgroundColor = UIColor.greenColor()
    } else if(score <= 29) {
       self.toolbar.backgroundColor = UIColor.yellowColor()
    } else if(score >= 30) {
        self.toolbar.backgroundColor = UIColor.redColor()
    }
    if (score <= 0) {
        self.scoreshow.text = "0"
    } else {
        self.scoreshow.text = (score as NSNumber).stringValue }
}

then it's called everytime the table view is tapped with this :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

thing is, it always worked ever since I built the App, but on iOS 10, it doesn't. simply put the color does not change...

Any Help would be appreciated

Jp4Real
  • 1,982
  • 4
  • 18
  • 33

7 Answers7

27

I was able to solve it, it's changed to this : BarTintColor, found it myself couldn't find anything in apple docs mentionning it

Use it like this : self.toolbar.barTintColor

edit : there is sometinhg mentionning it now in the GM release docs, see answer bellow

Jp4Real
  • 1,982
  • 4
  • 18
  • 33
  • Like I said, just post the part of you code that doesn't work, I don' know swift 3 but from what i red i could be as simple as having it caleed BarTint instead of barTintColor – Jp4Real Sep 24 '16 at 13:13
  • I just finished converting my app to swift 3 and the answer above works on swift 3 – Jp4Real Nov 08 '16 at 20:19
  • 1
    works in swift 4, just need to reference the barTintColor property instead of background color property on your outlet. – nbpeth Dec 28 '17 at 21:06
5

This is working in xCode 11 for IOS12 and Swift 4:

            self.navigationController?.toolbar.barTintColor = UIColor.black
Larry Ricker
  • 199
  • 1
  • 7
3

Using barTintColor does not result in the proper color I want, but the following approach worked for me in iOS 11 / Swift 4.

First, create an image of any size that a solid color of what you would like your bar to be.

After that, use the following extension:

import UIKit

extension UIToolbar {
    func setBackgroundColor(image: UIImage) {
        setBackgroundImage(image, forToolbarPosition: .any, barMetrics: .default)
    }
}

The solid color image you created will then be applied as the background of your toolbar, and should perfectly match your desired color.

In this example, I used a 1x1 image with purple: enter image description here

CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • This was the only solution that worked. Setting .tint, and .barTint did nothing, but this is completely against the apple docs. Would this qualify for radar? – user1603721 Jul 19 '18 at 01:09
3

For iOS 11 & 12 Swift 4 I'm able to do it with this code:

self.navigationController?.toolbar.barTintColor = UIColor.init(white: 0.9, alpha: 1)
chhay sotheara
  • 349
  • 2
  • 4
2

I managed to make this work on IOS 10 and Swift 3 with this code:

let dummyToolbar = UIToolbar()
dummyToolbar.barTintColor = .lightGray
dummyToolbar.sizeToFit() // without this line it doesn't work
Adrian
  • 19,440
  • 34
  • 112
  • 219
1

Change background colour:

toolBar.barTintColor = UIColor.musalaDarkBlueColor

Change buttons colour:

toolBar.tintColor = UIColor.white
zdravko zdravkin
  • 2,090
  • 19
  • 21
1

As of iOS 15.5, Apple's UIToolBar documentation is confusing in this regard, providing only a pretty obscure hint, that .isTranslucent has something to do with bar color, and is true by default. The missing fact is .isTranslucent overrides .barTintColor, and needs to be explicitly set to false before setting .barTintColor will have any effect.

// 
// Create toolbar with transparent background:
//
let image1 = UIImage(named:      "lolImage")!.withRenderingMode(.alwaysOriginal)
let image2 = UIImage(systemName: "x.square")

let button1  = UIBarButtonItem(image: image1 , style: .plain, target: nil, action: nil)
let button2  = UIBarButtonItem(image: image2 , style: .plain, target: nil, action: nil)

let toolbar = UIToolbar()
toolbar.items = [button1, button2]
toolbar.isTranslucent = false   ⇦ ⇦ ⇦ ⇦  IMPORTANT!!!
toolbar.barTintColor  = .clear
clearlight
  • 12,255
  • 11
  • 57
  • 75