Search code examples
swiftuikitprogress-bar

progressview in TableViewCell doesn't work


enter image description hereI made a custom cell that has progressView in it. I set the timer and every second it updates progressbar. Everything works fine when I use color such as UIcolor.red, UIcolor.green.

The problem occurs when I modify its color or use custom color creaded by hexcode. The progressBar starts at 0.25 point and until it goes beyond that point, it doesn't update.

enter image description here This is unexpected behavior.

unexpected behavior. It should not have the same progress until it is full

import UIKit


class ViewController: UITableViewController {[![enter image description here][3]][3]
    
    
    var time = 0
    var progressNumber:Float = 0.2
    let dummyArray = ["Pull Up","IOS","Strecthing","Bench Press"]
    override func viewDidLoad() {
        super.viewDidLoad()

        
        tableView.register(UINib(nibName: "progreeCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
        tableView.rowHeight = 60
        
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
    }
    
    @objc func updateTime() {

        progressNumber += 0.1
       
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
       
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        progressNumber += 0.03
        tableView.reloadData()
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dummyArray.count
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! progreeCell
        
        
        
        cell.textLabel?.text = dummyArray[indexPath.row]

        
        cell.progressBar.progress = progressNumber / Float(indexPath.row + 1)
        cell.progressBar.alpha = 0.5
        
        cell.progressBar.progressViewStyle = .bar
        
        let color = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1)

        
        
        
        cell.progressBar.progressTintColor = color
        
        

        
        
        return cell
    }
    
    
    
    
    
}

Solution

  • Some searching indicates this is a bug in UIProgressView...

    • IF ...
      • the progress view Height is > 20
    • and
      • we call progressBar.progressTintColor = color

    we see the issue.

    Since your UI design requires the progress view Height to be greater than 20, you can avoid the problem by using .tintColor instead:

    cell.progressBar.tintColor = color
    

    As a side note... many "built-in" UI controls have quirks (and bugs). You may want to swap your UIProgressView out for a normal UIView and update its width constraint.