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

[self setTitle:text forState:UIControlStateNormal];

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


当前回答

我得到了它的工作组合的答案:

[[[button titleLabel] layer] removeAllAnimations];

    [UIView performWithoutAnimation:^{

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

    }];

其他回答

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

}

不理想,但还行。

你实际上可以在动画块之外设置标题,只是要确保在performWithoutAnimation中调用layoutIfNeeded():

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

如果你有一堆按钮,考虑在超视图上调用layoutIfNeeded():

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

斯威夫特5

myButton.titleLabel?.text = "title"
myButton.setTitle("title", for: .normal)

将按钮类型更改为自定义表单界面生成器。

这对我很管用。

我发现这个解决方案与UIButtonTypeSystem一样,但只有当按钮出于某种原因被启用时才会工作。

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

因此,如果您需要在设置标题时禁用按钮,则必须添加这些。

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

(iOS 7, Xcode 5)