在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
在ios7中,我的UIButton标题在错误的时间动画进出——迟到。在iOS 6上不出现此问题。我用的是:
[self setTitle:text forState:UIControlStateNormal];
我更希望这能立即发生,而不是一个空白的框架。这种眨眼特别分散注意力,并将注意力从其他动画中转移开。
当前回答
从iOS 7.1开始,唯一适合我的解决方案是用UIButtonTypeCustom类型初始化按钮。
其他回答
这适用于自定义按钮:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
对于系统按钮,你需要在重新启用动画之前添加这个(谢谢@Klaas):
[_button layoutIfNeeded];
请注意:
当_button的"buttonType"为"UIButtonTypeSystem"时,以下代码无效:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
当_button的"buttonType"为"UIButtonTypeCustom"时,上述代码有效。
Swift 4版Xhacker刘回答
import Foundation
import UIKit
extension UIButton {
func setTitleWithOutAnimation(title: String?) {
UIView.setAnimationsEnabled(false)
setTitle(title, for: .normal)
layoutIfNeeded()
UIView.setAnimationsEnabled(true)
}
}
你可以简单地创建自定义按钮,它将停止动画而改变标题。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"the title" forState:UIControlStateNormal];
你也可以在Storyboard复选框中完成: 选择storyboard中的按钮->选择属性检查器(左边第四个)->在“类型”下拉菜单中,选择“自定义”而不是可能被选中的“系统”。
好运!
从iOS 7.1开始,唯一适合我的解决方案是用UIButtonTypeCustom类型初始化按钮。