So, I have a UIProgressView
in a basic expense tracking app and I want to change its progress tint as it reaches the end from green to red progressively. I don’t really know how to do this (I’m just starting out swift and Xcode).
This is how it should roughly work:
My progress is calculated with this line:
let progress = Float(UserDefaults.standard.double(forKey: "spentThisMonth") / UserDefaults.standard.double(forKey: "monthlyExpenseLimit"))
progressView.progress = progress
Here I am dividing the amount the user has spent this month from the limit to get the progress. As the user approaches the limit, the progress tint turns red to indicate that the user should lower their expenditure.
This question is similar but I think its Objective-C, while I use Swift 5. Is there any way to do this, i.e is it possible? I am also new to Stack Overflow so I apologise in advance if I’m doing something wrong.
2
Answers
Sure it can be done using Swift. Here the code from answer made in Swift:
You can set the green / red tint color easily by using the
progress
value you’ve already calculated:Here’s a working example – each time you tap the screen, the progress will increment until it reaches 100%, at which time it will decrement back to Zero: