IOS 10, the background color of the UIToolbar does not change

I have an application that has been since iOS 8. the way it works is that you have a UITableView and click on the cells to create a rating, depending on the rating, 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 is called every time the table view looks like this:

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

thing, it always worked since I built the application, but on iOS 10 it is not. just put the color will not change ...

Any help would be appreciated

+4
source share
4 answers

, : BarTintColor, , ,

: self.toolbar.barTintColor

edit: sometinhg, GM, .

+9

IOS 10 Swift 3 :

let dummyToolbar = UIToolbar()
dummyToolbar.barTintColor = .lightGray
dummyToolbar.sizeToFit() // without this line it doesn't work
+1

barTintColor , , iOS 11/Swift 4.

, , , .

After that, use the following extension:

import UIKit

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

The created solid color image will be applied as the background of your toolbar and should ideally match your desired color.

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

0
source

Source: https://habr.com/ru/post/1652225/


All Articles