在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
当前回答
你实际上可以在动画块之外设置标题,只是要确保在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()
}
其他回答
将按钮类型更改为自定义表单界面生成器。
这对我很管用。
结合以上的答案,可以为UIButtonTypeSystem提供以下解决方案:
if (_button.enabled)
{
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
}
else // disabled
{
[UIView setAnimationsEnabled:NO];
_button.enabled = YES;
[_button setTitle:@"title" forState:UIControlStateNormal];
_button.enabled = NO;
[UIView setAnimationsEnabled:YES];
}
具有系统类型的UIButton在setTitle(_:for:)上有隐式动画。你可以用两种方法来解决这个问题:
设置按钮类型为自定义,从代码或接口生成器:
let button = UIButton(type: .custom)
从代码中禁用动画:
UIView.performWithoutAnimation {
button.setTitle(title, for: .normal)
button.layoutIfNeeded()
}
在Swift中,你可以使用:
UIView.performWithoutAnimation {
self.someButtonButton.setTitle(newTitle, forState: .normal)
self.someButtonButton.layoutIfNeeded()
}
斯威夫特5
myButton.titleLabel?.text = "title"
myButton.setTitle("title", for: .normal)