Reputation: 2002
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
Upvotes: 13
Views: 13795
Reputation: 12615
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
Upvotes: 1
Reputation: 2378
Change background colour:
toolBar.barTintColor = UIColor.musalaDarkBlueColor
Change buttons colour:
toolBar.tintColor = UIColor.white
Upvotes: 1
Reputation: 319
This is working in xCode 11 for IOS12 and Swift 4:
self.navigationController?.toolbar.barTintColor = UIColor.black
Upvotes: 5
Reputation: 399
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)
Upvotes: 3
Reputation: 36612
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:
Upvotes: 3
Reputation: 20058
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
Upvotes: 2
Reputation: 2002
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
Upvotes: 28