When I call setTitle on a UIButton, the button flashes in iOS 7. I tried setting myButton.highlighted = NO, but that didn\'t stop the button from flashing.
[myBu
Default "setTitle" behavior is definitely hateful!
My solution is:
[UIView setAnimationsEnabled:NO];
[_button layoutIfNeeded];
[_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Also, in storyboard, under button's property I uncheck:
And check:
Tested and working on iOS 8 too.
I suggest you to create your custom button and override setTitle method.
class UnflashingButton: UIButton {
override func setTitle(title: String?, forState state: UIControlState) {
UIView.setAnimationsEnabled(false)
self.layoutIfNeeded()
super.setTitle(title, forState: state)
UIView.setAnimationsEnabled(true)
}
}
Set the button type to UIButtonTypeCustom and it'll stop flashing
Try this, this works in newer versions of IOS:
class CustomButtonWithNoEffect : UIButton {
override func setTitle(_ title: String?, for state: UIControlState) {
UIView.performWithoutAnimation {
super.setTitle(title, for: state)
super.layoutIfNeeded()
}
}
}
You can simply just make another function for UIButton for easy future usage.
extension UIButton {
func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
UIView.performWithoutAnimation {
self.setTitle(title, for: controlState)
self.layoutIfNeeded()
}
}
}
That is it!
And just set the title as you would before but replace setTitle
with setTitleWithoutAnimation
Another trick
@IBOutlet private weak var button: UIButton! {
didSet {
button.setTitle(title, for: .normal)
}
}
This works only if you know the value of the title without making any API calls. The button's title will be set before the view is loaded so you won't see the animation
*Please note *
when "buttonType" of _button is "UIButtonTypeSystem", below code is invalid:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
when "buttonType" of _button is "UIButtonTypeCustom", above code is valid.