Usually simply setting the button type to Custom works for me, but for other reasons I needed to subclass UIButton and set the button type back to the default (System), so the blinking reappeared.
Setting UIView.setAnimationsEnabled(false)
before changing the title and then to true again after that didn't avoid the blinking for me, no matter if I called self.layoutIfNeeded()
or not.
This, and only this in the following exact order, worked for me with iOS 9 and 10 beta:
1) Create a subclass for UIButton (don't forget to set the custom class for the button in the Storyboard too).
2) Override setTitle:forState:
as follows:
override func setTitle(title: String?, forState state: UIControlState) {
UIView.performWithoutAnimation({
super.setTitle(title, forState: state)
self.layoutIfNeeded()
})
}
In Interface Builder, you can leave the button type to System, no need to change it to Custom Type for this approach to work.
I hope this helps someone else, I've struggled for so long with the annoying blinking buttons that I hope to avoid it to others ;)
self.button.titleLabel.text = text
. But this don't resize label frame and don't work with UIControlStates correctly – zxcat