How to stop unwanted UIButton animation on title change?

前端 未结 24 2733
面向向阳花
面向向阳花 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: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..

提交回复
热议问题