Search code examples
iosswiftxcodeuiprogressview

Unable to set progress bar progress twice in iOS?


I have UIProgressView.On begin to text edit for a UITextField I set the value of progress bar to 1 . Before that initially I make it progress to 0.1 .But it is setting progress only once. If I first set progress to 0.1 then after it does not set progress to 1.please tell me what is the issue ?

func textFieldDidBeginEditing(_ textField: UITextField) {
    setViewBottomColor()

    var view:BottomView?

    if textField == textFieldEmail {
        view = self.bottomViewFirst
        view?.trackTintColor = Constant.AppColor.viewBottom
        view?.progressTintColor = Constant.AppColor.purpleViewColor

    }
    else if textField == textFieldPassword {
        view = self.bottomViewSecond
        view?.trackTintColor = Constant.AppColor.viewBottom
        view?.progressTintColor = Constant.AppColor.purpleViewColor
    }
    if view != nil {
        view?.setProgress(1, animated: true)
        UIView.animate(withDuration: 1.0, animations: {
            view?.layoutIfNeeded()
        }, completion: { (finish) in
        })
}
}

func setViewBottomColor() {

    self.bottomViewFirst.trackTintColor = Constant.AppColor.viewBottom
    self.bottomViewFirst.progressTintColor = Constant.AppColor.purpleViewColor
    self.bottomViewFirst?.setProgress(0.1, animated: false)
    self.bottomViewFirst?.layoutIfNeeded()

}

Solution

  • Check below code snippet I have used to check. It is working fine.

    You may check few things:

    1. All IBOutlets connections must be there.
    2. If you are using custom UIProgressView as BottomView then you must changed both progress view class in xib/storyboard as well.
    3. No Need to have view animation if for progress value change only.

       func textFieldDidBeginEditing(_ textField: UITextField) {
      
          resetProgresses()
          var view = UIProgressView()
      
          if textField == t1 {
              view = self.progressBar
              view.trackTintColor = .red
              view.progressTintColor = .green
      
          }
          else if textField == t2 {
              view = self.progressBar2
              view.trackTintColor = .green //Constant.AppColor.viewBottom
              view.progressTintColor = .red//Constant.AppColor.purpleViewColor
          }
      
          if view != nil {
              view.setProgress(1, animated: true)
          }
      
        }
      
       func resetProgresses()  {
          self.progressBar?.setProgress(0.1, animated: true)
          self.progressBar2?.setProgress(0.1, animated: true)
      
        }