239

In iOS 7 my UIButton titles are animating in and out at the wrong time - late. This problem does not appear on iOS 6. I'm just using:

[self setTitle:text forState:UIControlStateNormal];

I would prefer this happens instantly and without a blank frame. This blink is especially distracting and draws attention away from other animations.

7
  • We're experiencing this as well. Not sure if it's a iOS7 bug or something we should fix.
    – Sway
    Sep 22, 2013 at 22:50
  • Try,[self.button setHighlighted:NO];
    – karthika
    Sep 23, 2013 at 4:58
  • Thanks for these ideas. I tried setHighlighted:NO, but no luck there. I am able to reduce the blink by placing setTitle inside: [UIView animateWithDuration:0.0f animations:^{ ... }];
    – exsulto
    Sep 23, 2013 at 13:48
  • 1
    You can use this workaround in some cases: self.button.titleLabel.text = text. But this don't resize label frame and don't work with UIControlStates correctly
    – zxcat
    Oct 8, 2013 at 11:58
  • That's a clever workaround. I'll play with this and see what happens, unfortunately I'm using UIControlStates.
    – exsulto
    Oct 9, 2013 at 14:46

24 Answers 24

290

Use the performWithoutAnimation: method and then force layout to happen immediately instead of later on.

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}];
5
  • 12
    This works as well as the accepted answer, but seems nicer because it is more encapsulated - it is impossible to forget to add the [UIView setAnimationsEnabled:YES], or for it to get removed down the track.
    – siburb
    Jun 12, 2014 at 6:51
  • 24
    It works for system buttons if you call [button layoutIfNeeded]; inside the block. Mar 3, 2015 at 8:10
  • 2
    BTW, for system buttons, layoutIfNeed should be called after text changed
    – YON
    May 6, 2015 at 13:56
  • This is the best solution! Cheers
    – sachadso
    Jan 14, 2016 at 11:31
  • This is correct one for me. It's most voted and on 6th place. Nice...
    – solgar
    May 6, 2020 at 6:26
169

This works for custom buttons:

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

For system buttons you need to add this before re-enabling animations (thank you @Klaas):

[_button layoutIfNeeded];
14
  • 14
    Unfortunately this doesn't seem to work. Neither does performWithoutAnimation
    – Sway
    Oct 24, 2013 at 22:23
  • 9
    Ok, so the fix that worked in the end was to leave the original UIButton text blank so that when I set it with code it doesn't trigger the animation.
    – Sway
    Oct 24, 2013 at 22:27
  • 30
    This works only if you set the button's type to custom, as per this answer stackoverflow.com/a/20718467/62. Feb 25, 2014 at 1:39
  • 15
    As of iOS 7.1 I had to add [_button layoutIfNeeded];
    – Klaas
    May 19, 2014 at 15:17
  • 7
    @LironYahdav if you have the button type set to UIButtonTypeCustom, then this answer is not required.
    – DonnaLea
    May 23, 2014 at 0:35
108

In Swift you can use :

UIView.performWithoutAnimation {
    self.someButtonButton.setTitle(newTitle, forState: .normal)
    self.someButtonButton.layoutIfNeeded()
}
5
  • 1
    This was by far the easiest method. And thank you for including a swift answer btw
    – simplexity
    Mar 10, 2016 at 15:28
  • 1
    Best answer for Swift!
    – Nubaslon
    Jun 2, 2017 at 11:08
  • Had an annoying bug where changing a UIButton title while offscreen would cause weird animation timings with interactivePopGestureRecognizer and this solved it. I still think it's a bug with the OS though Aug 7, 2017 at 2:05
  • Strange that .layoutIfNeeded() has to be called, but I tested it both ways in Swift 5 and it definitely still animates without it.
    – wildcat12
    Nov 8, 2019 at 14:08
  • 4
    Not really. If you don't call layoutIfNeeded() then the button is flagged as needing to be redrawn but this won't happen until the next layout pass, which will be outside the performWithoutAnimation
    – Paulw11
    Nov 8, 2019 at 18:57
97

Change button type to custom form interface builder.

enter image description here

This worked for me.

4
  • 11
    Best solution! Thank you. Jul 15, 2016 at 12:58
  • 4
    But this also disable animation on click button. I want only disable animation on showing button. Apr 12, 2017 at 7:06
  • This works if you don't care about the animation when the button is tapped. Aug 17, 2017 at 18:28
  • I've had a couple of buttons set like this and obviously this is the most elegant answer for my case. Nice, thanks!
    – Zoltán
    Aug 3, 2019 at 9:37
62

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.

2
  • Can't believe how much time I spent before figuring out you just need to change the button type... ugh... Sep 10, 2016 at 3:15
  • 1
    Works without any code. Change only buttons type and it'll work.
    – Alex Motor
    Feb 16, 2019 at 9:03
56

Starting in iOS 7.1 the only solution that worked for me was initializing the button with type UIButtonTypeCustom.

2
  • This is the most sensible approach for anyone that doesn't require UIButtonTypeSystem.
    – DonnaLea
    May 23, 2014 at 0:33
  • This ended up working best for me, I just created a CUSTOM button and made it look and highlight like a system button. Can hardly see the difference but you don't have that delay.
    – Travis M.
    Jun 27, 2014 at 18:20
22

Swift 5

myButton.titleLabel?.text = "title"
myButton.setTitle("title", for: .normal)
4
  • 3
    No idea why this works but it does and it's the cleanest solution. UIKit is weird. Jan 7, 2020 at 17:11
  • 1
    no, its not a clean solution, but its quite dirty, as this may change with any iOS Jan 29, 2021 at 16:03
  • @NathanHosselton see dubenko's answer on Apr 22'14. "in first we change title for button, then resize button for this title"
    – Brian Hong
    Feb 4, 2021 at 0:24
  • This is a workaround which can stop to work in any new versions of UIKit. UIButton of type "system" animates title update when new title is different from previous one. If you find it awkward then you should use a button of type "custom" Jan 7, 2022 at 23:09
18

so i find worked solution:

_logoutButton.titleLabel.text = NSLocalizedString(@"Logout",);
[_logoutButton setTitle:_logoutButton.titleLabel.text forState:UIControlStateNormal];

in first we change title for button, then resize button for this title

4
  • 1
    I use the same workaround. The accepted answer doesn't work for me.
    – deej
    Aug 10, 2014 at 14:52
  • This causes the title to flash twice, at least with iOS 8.
    – Jordan H
    Dec 27, 2014 at 5:03
  • 1
    This works for me in both 7.1 and 8.1 without flashing. Simple and effective.
    – Todd
    Feb 10, 2015 at 4:24
  • Works perfectly in iOS 11, although I had to use the same string again for the second line (using the button label's title caused it to blink).
    – SilverWolf
    Apr 19, 2018 at 16:42
18

UIButton with system type has implicit animation on setTitle(_:for:). You can fix it two different ways:

  1. Set the button type to custom, either from code or Interface Builder:
let button = UIButton(type: .custom)

enter image description here

  1. Disable animation from code:
UIView.performWithoutAnimation {
    button.setTitle(title, for: .normal)
    button.layoutIfNeeded()
}
0
14

Set the button type to UIButtonTypeCustom and it'll stop flashing

1
  • How can all those workaround "solutions" have so many upvotes when this simple answer must solve this issue 99% of the time...
    – Rob
    Apr 25, 2019 at 7:19
14

I’ve made a Swift extension to do 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.

(The code is for Swift 2, Swift 3 and Objective-C should be similar)

1
  • Not going to use it now but very handy to have around! Aug 24, 2016 at 1:33
12

Set UIButton type as Custom. That should remove fade in and out animations.

1
  • 1
    This should have more upvotes! Works perfectly and disables the animation at the root cause, instead of these other workarounds. Feb 5, 2019 at 12:53
9

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 ;)

1
  • Don't forget layoutIfNeeded() :]
    – Tai Le
    Nov 16, 2018 at 6:55
6

You can simply create Custom button and it will stop animate while changing the title.

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"the title" forState:UIControlStateNormal];

you also can do it in Storyboard checkbox: select the button in storyboard -> select the attributes inspector (fourth from left side) -> in the 'Type' drop down menu, select 'Custom' instead of 'System' that was probably selected.

Good luck!

4

Swift 4 version of Xhacker Liu answer

import Foundation
import UIKit
extension UIButton {
    func setTitleWithOutAnimation(title: String?) {
        UIView.setAnimationsEnabled(false)

        setTitle(title, for: .normal)

        layoutIfNeeded()
        UIView.setAnimationsEnabled(true)
    }
} 
3

You can remove the animations from from the title label's layer:

    [[[theButton titleLabel] layer] removeAllAnimations];
3
  • I went through all the answers. This one is the best. Jul 4, 2014 at 16:31
  • 2
    It still blinks but it's better.
    – Lucien
    Nov 25, 2014 at 15:38
  • THIS SHOULD BE THE ANSWER.
    – mxcl
    Mar 30, 2018 at 19:30
1

You can actually set the title outside of an animation block, just be sure to call layoutIfNeeded() inside a performWithoutAnimation:

button1.setTitle("abc", forState: .Normal)
button2.setTitle("abc", forState: .Normal)
button3.setTitle("abc", forState: .Normal)
UIView.performWithoutAnimation {
    self.button1.layoutIfNeeded()
    self.button2.layoutIfNeeded()
    self.button3.layoutIfNeeded()
}

If you have a bunch of buttons, consider just calling layoutIfNeeded() on the super view:

button1.setTitle("abc", forState: .Normal)
button2.setTitle("abc", forState: .Normal)
button3.setTitle("abc", forState: .Normal)
UIView.performWithoutAnimation {
    self.view.layoutIfNeeded()
}
0

I've found that this workaround works with UIButtonTypeSystem as well but will only work if the button is enabled for some reason.

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

So you'll have to add these if you need the button to be disabled when setting its title.

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

(iOS 7, Xcode 5)

4
  • Just confirmed that this workaround no longer works on iOS 7.1.
    – sCha
    Mar 13, 2014 at 17:13
  • don't suppose you've found a solution for 7.1? Mar 20, 2014 at 15:24
  • @GeorgeGreen couldn't find any working solutions for UIButtonTypeSystem. I had to use UIButtonTypeCustom.
    – sCha
    Mar 21, 2014 at 5:31
  • Since 7.1, you'll need to apply title changes to all states, setting it just for the normal state no longer applies. [_button setTitle:@"title" forState:UIControlStateDisabled]
    – Sam
    Apr 2, 2014 at 16:30
0

Combining above great answers results in following workaround for UIButtonTypeSystem:

if (_button.enabled)
{
    [UIView setAnimationsEnabled:NO];
    [_button setTitle:@"title" forState:UIControlStateNormal];
    [UIView setAnimationsEnabled:YES];
}
else // disabled
{
    [UIView setAnimationsEnabled:NO];
    _button.enabled = YES;
    [_button setTitle:@"title" forState:UIControlStateNormal];
    _button.enabled = NO;
    [UIView setAnimationsEnabled:YES];
}
0

I got the ugly animation problem when changing button titles in view controllers within a UITabBarController. The titles that were originally set in the storyboard showed up for a short while before fading into their new values.

I wanted to iterate through all subviews and use the button titles as keys to get their localized values with NSLocalizedString, such as;

for(UIView *v in view.subviews) {

    if ([v isKindOfClass:[UIButton class]]) {
        UIButton *btn = (UIButton*)v;
        NSString *newTitle = NSLocalizedString(btn.titleLabel.text, nil);
        [btn setTitle:newTitle];
    }

}

I found out that what's triggering the animation is really the call to btn.titleLabel.text. So to still make use of the storyboards and have the components dynamically localized like this I make sure to set every button's Restoration ID (in Identity Inspector) to the same as the title and use that as key instead of the title;

for(UIView *v in view.subviews) {

    if ([v isKindOfClass:[UIButton class]]) {
        UIButton *btn = (UIButton*)v;
        NSString *newTitle = NSLocalizedString(btn.restorationIdentifier, nil);
        [btn setTitle:newTitle];
    }

}

Not ideal, but works..

0

The Xhacker Liu extension converted to Swift 3 :

extension UIButton {
    func setTitleWithoutAnimation(title: String?) {
        UIView.setAnimationsEnabled(false)

        setTitle(title, for: .normal)

        layoutIfNeeded()
        UIView.setAnimationsEnabled(true)
    }
}
0

A convenient extension for animated button title change in Swift that plays nicely with the default implementation:

import UIKit

extension UIButton {
  /// By default iOS animated the title change, which is not desirable in reusable views
  func setTitle(_ title: String?, for controlState: UIControlState, animated: Bool = true) {
    if animated {
      setTitle(title, for: controlState)
    } else {
      UIView.setAnimationsEnabled(false)
      setTitle(title, for: controlState)
      layoutIfNeeded()
      UIView.setAnimationsEnabled(true)
    }
  }
}
1
  • @Fogmeister 1. My answer is different 2. Up-to-date Swift syntax 3. Consistent with Apple's API for UIButton. Aug 22, 2018 at 13:48
-1

I got it to work with a combination of answers:

[[[button titleLabel] layer] removeAllAnimations];

    [UIView performWithoutAnimation:^{

        [button setTitle:@"Title" forState:UIControlStateNormal];

    }];
-2

Maybe generating 2 animations and 2 buttons is a better solution, to avoid the problem that is appearing with animating and changing the text of a button?

I created a second uibutton and generated 2 animation, this solution works with no hickups.

    _button2.hidden = TRUE;
    _button1.hidden = FALSE;

    CGPoint startLocation = CGPointMake(_button1.center.x, button1.center.y - 70);
    CGPoint stopLocation  = CGPointMake(_button2.center.x, button2.center.y- 70);


    [UIView animateWithDuration:0.3 animations:^{ _button2.center = stopLocation;} completion:^(BOOL finished){_button2.center = stopLocation;}];
    [UIView animateWithDuration:0.3 animations:^{ _button1.center = startLocation;} completion:^(BOOL finished){_button1.center = startLocation;}];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.