Disable UIButton setTitle: forState: animation

I use NSTimer to update the UIButton header every second.

It works, but the text in the header automatically blinks (animated to alpha 0 and vice versa).

I tried using button.layer.removeAllAnimations() with no luck and no exceptions, so QuartzCore seems to be connected correctly.


Current idle paranoid code:

 UIView.setAnimationsEnabled(false) UIView.performWithoutAnimation { button.setTitle(time, forState: .Normal) button.layer.removeAllAnimations() } UIView.setAnimationsEnabled(true) 
+7
ios objective-c animation swift uibutton
source share
3 answers

Make sure your button is "custom" and not "system".

If you created it on a storyboard, just change the type of button. If you created it programmatically, then it should be:

 UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
+29
source share

Ive made a Swift extension for this:

 extension UIButton { func setTitleWithOutAnimation(title: String?) { UIView.setAnimationsEnabled(false) setTitle(title, forState: .Normal) layoutIfNeeded() UIView.setAnimationsEnabled(true) } } 

Works for me on iOS 8 and 9, with UIButtonTypeSystem .

+13
source share

You can make changes to the buttons inside the closure:

 UIView.performWithoutAnimation { //Button changes button.layoutIfNeeded() } 

Hope this helps.

+4
source share

All Articles