8

I want to make UIProgressBar programmatically in swift?

what is wrong in this code?

 override func viewDidLoad() {
    super.viewDidLoad()
        let progressView = UIProgressView(progressViewStyle: .Bar)
        progressView.center = self.view.center
        progressView.frame = CGRectMake(0,0,50,20)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        progressView.setProgress(0.5, animated: false)
        self.view.addSubview(progressView)
 }
sulabh qg
  • 1,155
  • 2
  • 12
  • 20
  • 1
    You said `translatesAutoresizingMaskIntoConstraints = false`, but then didn't add any constraints. Just add constraints to dictate where you want this. If you look at the view debugger, I bet this view is not where you expect it to be... – Rob Jun 08 '16 at 06:52
  • thank you @Rob , it is working fine. – sulabh qg Jun 08 '16 at 06:55

3 Answers3

11
let progressView = UIProgressView(progressViewStyle: .Bar)
progressView.center = view.center
progressView.setProgress(0.5, animated: true)
progressView.trackTintColor = UIColor.lightGrayColor()
progressView.tintColor = UIColor.blueColor()
view.addSubview(progressView)

you need to do this type code and you can't need to set frame .

aturan23
  • 4,798
  • 4
  • 28
  • 52
0

You need to:

  1. Add subview of your progressView
  2. Set up the constraints, since you make translateAutoresizingMaskIntoConstraints = false
0

Swift 5:

let progressView = UIProgressView(progressViewStyle: .bar)
progressView.center = view.center
progressView.setProgress(0.5, animated: true)
progressView.trackTintColor = .lightGray
progressView.tintColor = .blue
view.addSubview(progressView)
Matt Andrzejczuk
  • 2,001
  • 9
  • 36
  • 51