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

[self setTitle:text 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()
}

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

[[[button titleLabel] layer] removeAllAnimations];

    [UIView performWithoutAnimation:^{

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

    }];

使用performWithoutAnimation:方法,然后强制布局立即发生,而不是稍后。

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}];

从iOS 7.1开始,唯一适合我的解决方案是用UIButtonTypeCustom类型初始化按钮。

将按钮类型设置为UIButtonTypeCustom,它将停止闪烁