How to stop unwanted UIButton animation on title change?

前端 未结 24 2547
面向向阳花
面向向阳花 2020-11-28 19:07

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 fo         


        
相关标签:
24条回答
  • 2020-11-28 19:29

    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)

    0 讨论(0)
  • 2020-11-28 19:32

    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!

    0 讨论(0)
  • 2020-11-28 19:34

    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)
        }
    } 
    
    0 讨论(0)
  • 2020-11-28 19:34

    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 讨论(0)
  • 2020-11-28 19:35

    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;}];
    
    0 讨论(0)
  • 2020-11-28 19:37

    In Swift you can use :

    UIView.performWithoutAnimation {
        self.someButtonButton.setTitle(newTitle, forState: .normal)
        self.someButtonButton.layoutIfNeeded()
    }
    
    0 讨论(0)
提交回复
热议问题