Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button text flashes when title is set using a timer function, Swift

I am following the answer shown here in stackoverflow to implement a countdown timer. The timer text does not flash (works perfectly) when the text is set for a label.

When the same text is set for a button it flashes every time the title is set. How can avoid the button text flashing ?

@IBOutlet weak var countDownTimer: UILabel!
@IBOutlet weak var countDownTimerButton: UIButton!
var count = 120

override func viewDidLoad() {
    super.viewDidLoad()

    var _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)
    // Do any additional setup after loading the view.
}


func update() {

    if(count > 0){
        let minutes = String(count / 60)
        let seconds = String(count % 60)
        countDownTimer.text = minutes + ":" + seconds // Setting text for label (Works perfectly)

        let text = minutes + ":" + seconds
        countDownTimerButton.setTitle(text, for: .normal) // Setting text for button, (Text flashes everytime it is set)
        count -= 1
    }

}
like image 854
SpaceX Avatar asked Jan 19 '26 18:01

SpaceX


1 Answers

This is caused through the setting of the button title text. Just set the type of the button to custom and the flashing should stop

like image 91
amor91 Avatar answered Jan 22 '26 10:01

amor91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!