Search code examples
swiftuitabbarcontrolleruiprogressview

ProgressView UI not showing but the Label is. [Swift]


I want to have my progressView in all tabBar view controllers so the user can use the app while seeing how much the data is loading.

The problem I have is the progressView UI is not showing only the label. I think I set everything correctly and check if it is nil (which it wasn't). Please help thanks.

Only label is showing

TabBarViewController

import UIKit

let progressViewTag = 10000
let progressUpdateNotification = "progressUpdateNotification"
var progressLabel = UILabel()

class TabBarViewController: UITabBarController {
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let progressView = UIProgressView(progressViewStyle: .bar)
        progressView.tag = progressViewTag
        progressView.frame = CGRect(x: 0, y: 93, width: self.view.frame.width, height: 5)
        progressView.transform = progressView.transform.scaledBy(x: 1, y: 5)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(progressView)
        
        progressLabel = UILabel(frame: CGRect(x: 0, y: 91, width: UIScreen.main.bounds.width, height: 8))
        progressLabel.translatesAutoresizingMaskIntoConstraints = true
        progressLabel.textAlignment = .center
        progressLabel.font = UIFont.boldSystemFont(ofSize: 8)
        progressLabel.textColor = UIColor.white
        self.view.addSubview(progressLabel)
        
        progressView.setProgress(0.0, animated: false)
        
        NotificationCenter.default.addObserver(self, selector: #selector(didReceiveNotification(notification:)), name: NSNotification.Name(rawValue: progressUpdateNotification), object: nil)
    }
    
    var progressView: UIProgressView? {
        return self.view.viewWithTag(progressViewTag) as? UIProgressView
    }
    
    @objc func didReceiveNotification(notification: NSNotification) {
        if let progress = notification.object as? ProgressNotification {
            if progress.current == progress.total {
                progressLabel.text = ""
                self.progressView?.setProgress(0.0, animated: false)
                
            } else {
                let perc = Float(progress.current) / Float(progress.total)
                progressLabel.text = "Scraping \(String(format: "%.0f", (perc * 100))) % ..."
                self.progressView?.setProgress(perc, animated: true)
            }
        }
    }
}

class ProgressNotification {
    var current: Int = 0
    var total:   Int = 0
}

Server call with Notification

func apiCall(sender: UIViewController, counter: Int, completion: (()-> Void)? = nil) {
var count = counter
let total = 5
let notification = ProgressNotification()

counting(completion: { result in
    count += 1
    notification.current = count
    notification.total = total
    DispatchQueue.main.async {
        NotificationCenter.default.post(name:Notification.Name(rawValue: progressUpdateNotification), object: notification)
        print("notificationCenter called.")
    }
    
    .
    .
    .
    }
}

Solution

  •     progressView.translatesAutoresizingMaskIntoConstraints = false
    

    change it to.

        progressView.translatesAutoresizingMaskIntoConstraints = true
    

    also update your code for UIProgressView

    var progressView: UIProgressView? {
        return self.view.viewWithTag(progressViewTag) as? UIProgressView
    }
    

    change it to.

    var progressView : UIProgressView?
    
    class TabBarViewController: UITabBarController {
       
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.progressView = UIProgressView(progressViewStyle: .bar)
    }