How to prevent UIButton blinking when updating header

When I call setTitle on a UIButton, the button blinks in iOS 7. I tried setting myButton.highlighted = NO, but that did not stop the button from blinking.

[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal]; myButton.highlighted = NO; 

Here's how I set up a timer that updated the names:

 - (void)actionTimer { if (myTimer == nil) { myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(showActivity) userInfo: nil repeats: YES]; } } 

Here is a method that actually updates the names:

 - (void)showActivity { NSString *sym = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]; if (pauseInterval == nil) { // Update clock seconds = [[NSDate date] timeIntervalSinceDate:startInterval] - breakTime; // Update total earned secRate = rate.value / 60 / 60; total = secRate * seconds; [totalLabel setTitle:[NSString stringWithFormat:@"%@%.4f",sym,total] forState:UIControlStateNormal]; days = seconds / (60 * 60 * 24); seconds -= days * (60 * 60 * 24); int hours = seconds / (60 * 60); fhours = (float)seconds / (60.0 * 60.0); seconds -= hours * (60 * 60); int minutes = seconds / 60; seconds -= minutes * 60; // Update the timer clock [elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal]; } } 
+72
ios7 uibutton
Oct. 15 '13 at 0:42
source share
10 answers

Blinking is not caused by the timer as such. The UIButton setTitle: forState method flashes. One simple job is to use an untitled UIButton and overlay a UILabel. When you change the text of UILabel, it will not blink.

+16
Oct 22
source share

Set the button type to UIButtonTypeCustom and it will stop blinking

+168
Aug 02 '14 at
source share

A better approach than [UIView setAnimationsEnabled:NO] which can affect other animations is to disable only animations of a particular title.

Objective-C:

 [UIView performWithoutAnimation:^{ [myButton setTitle:text forState:UIControlStateNormal]; [myButton layoutIfNeeded]; }]; 

Swift:

 UIView.performWithoutAnimation { myButton.setTitle(text, for: .normal) myButton.layoutIfNeeded() } 
+34
Aug 22 '16 at 4:57
source share

* Note: *

when the "buttonType" button is _ "UIButtonTypeSystem" , the below code is invalid :

 [UIView setAnimationsEnabled:NO]; [_button setTitle:@"title" forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES]; 

when "buttonType" of_button is "UIButtonTypeCustom" , the code above is valid .

+29
Dec 21 '13 at 11:32
source share

See answers. How do I stop an unwanted UIButton animation when a name changes? :

 [UIView setAnimationsEnabled:NO]; [elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES]; 
+17
Nov 01 '13 at 4:54
source share

By default, "setTitle" behavior is definitely hateful!

My decision:

 [UIView setAnimationsEnabled:NO]; [_button layoutIfNeeded]; [_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal]; [UIView setAnimationsEnabled:YES]; 

In addition, in the storyboard under the button property, clear the check box:

  • Deselects
  • Highlighting

And check:

  • Disabled Adjust Image

Tested and works with iOS 8 .

Update - Swift

I suggest you create your custom button and override the 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) } } 
+6
Dec 18 '14 at 2:08
source share

You can simply make another function for UIButton for convenient future use.

 extension UIButton { func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) { UIView.performWithoutAnimation { self.setTitle(title, for: controlState) self.layoutIfNeeded() } } } 

That's all!

And just set the title as before, but replace setTitle with setTitleWithoutAnimation

+6
Jun 19 '17 at 23:36 on
source share

Try this, it works on newer versions of iOS:

 class CustomButtonWithNoEffect : UIButton { override func setTitle(_ title: String?, for state: UIControlState) { UIView.performWithoutAnimation { super.setTitle(title, for: state) super.layoutIfNeeded() } } } 
+1
Sep 27 '18 at 20:10
source share

I'm new to programming and Stackoverflow, so I don't have enough reputation to comment directly to deploy https://stackoverflow.com/users/4673064/daniel-tseng an excellent answer. So I have to write my new answer, and it is like this:

 extension UIButton { func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) { UIView.performWithoutAnimation { self.setTitle(title, for: controlState) self.layoutIfNeeded() } } } 

Works great except for:

If all my subsequent calls in the "setTitleWithoutAnimation" code do not indicate the sender, then I get these strange messages related to CoreMedia or CoreData, for example, "Failed to inherit CoreMedia permissions from 2526: (null)"

This is probably quite the basis for coding and iOS, but for me, as a new coder, he sent me on a rabbit path for a while, like in: Today, the extension failed to inherit CoreMedia permissions , from where people had interesting answers, but it’s NOT reach the root of my problem.

So, I finally found that I need to go through all the parameters of my function without parameters, that is, I needed to specify the sender. This sender can be UIButton, Any, or AnyObject. It all worked, but in the end, there is a conflict between adding the button extension with "self" and the subsequent concrete expression that the button uses it.

Again, perhaps the main one, but new to me, so I decided it was worth sharing.

0
Apr 05 '18 at 22:39
source share

One more trick

 @IBOutlet private weak var button: UIButton! { didSet { button.setTitle(title, for: .normal) } } 

This only works if you know the value of the header without any API calls. The button title will be set before loading the view, so you will not see the animation

0
Jan 09 '19 at 21:21
source share



All Articles