在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:

[self setTitle:text forState:UIControlStateNormal];

我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。


当前回答

你可以从标题标签层中删除动画:

    [[[theButton titleLabel] layer] removeAllAnimations];

其他回答

你可以简单地创建自定义按钮,它将停止动画而改变标题。

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

你也可以在Storyboard复选框中完成: 选择storyboard中的按钮->选择属性检查器(左边第四个)->在“类型”下拉菜单中,选择“自定义”而不是可能被选中的“系统”。

好运!

当在UITabBarController中改变视图控制器中的按钮标题时,我得到了丑陋的动画问题。 最初在故事板中设置的标题在消失为新值之前会显示一小段时间。

我想遍历所有子视图,并使用按钮标题作为键来获得它们的本地化值与NSLocalizedString,如;

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];
    }

}

我发现触发动画的是对btn。titlabel。text的调用。 因此,为了仍然使用故事板,并像这样动态本地化组件,我确保将每个按钮的还原ID(在身份检查器中)设置为与标题相同,并将其用作键而不是标题;

for(UIView *v in view.subviews) {

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

}

不理想,但还行。

请注意:

当_button的"buttonType"为"UIButtonTypeSystem"时,以下代码无效:

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

当_button的"buttonType"为"UIButtonTypeCustom"时,上述代码有效。

设置UIButton类型为自定义。这应该会删除淡入和淡出动画。

一个方便的扩展动画按钮标题变化在Swift,发挥与默认实现很好:

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