iOS 10, UIToolbar background color does not change
Asked Answered
H

7

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

Hade answered 23/8, 2016 at 17:10 Comment(3)
same here - did you find a solution?Decompress
Anyone able to solve this issue?Vagrom
See my answer aboveHade
H
28

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

Hade answered 17/9, 2016 at 22:7 Comment(3)
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 barTintColorHade
I just finished converting my app to swift 3 and the answer above works on swift 3Hade
works in swift 4, just need to reference the barTintColor property instead of background color property on your outlet.Hectogram
R
5

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

            self.navigationController?.toolbar.barTintColor = UIColor.black
Rumney answered 6/11, 2019 at 1:5 Comment(0)
G
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

Gaston answered 20/3, 2018 at 5:9 Comment(1)
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?Eglanteen
T
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)
Totemism answered 17/10, 2018 at 8:24 Comment(0)
R
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
Rags answered 28/4, 2017 at 8:13 Comment(0)
A
1

Change background colour:

toolBar.barTintColor = UIColor.musalaDarkBlueColor

Change buttons colour:

toolBar.tintColor = UIColor.white
Autogenous answered 6/4, 2021 at 18:22 Comment(0)
I
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
Idaho answered 12/9, 2022 at 19:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.