objective-c 如何在标题更改时停止不需要的 UIButton 动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18946490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop unwanted UIButton animation on title change?
提问by exsulto
In iOS 7 my UIButton titles are animating in and out at the wrong time - late. This problem does not appear on iOS 6. I'm just using:
在 iOS 7 中,我的 UIButton 标题在错误的时间进出动画 - 晚了。这个问题在 iOS 6 上不会出现。我只是在使用:
[self setTitle:text forState:UIControlStateNormal];
I would prefer this happens instantly and without a blank frame. This blink is especially distracting and draws attention away from other animations.
我希望这会立即发生并且没有空白帧。这种眨眼尤其会分散注意力,并会吸引其他动画的注意力。
采纳答案by Jacob K
This works for custom buttons:
这适用于自定义按钮:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
For system buttons you need to add this before re-enabling animations (thank you @Klaas):
对于系统按钮,您需要在重新启用动画之前添加它(谢谢@Klaas):
[_button layoutIfNeeded];
回答by Snowman
Use the performWithoutAnimation:method and then force layout to happen immediately instead of later on.
使用该performWithoutAnimation:方法,然后强制布局立即发生而不是稍后发生。
[UIView performWithoutAnimation:^{
[self.myButton setTitle:text forState:UIControlStateNormal];
[self.myButton layoutIfNeeded];
}];
回答by Paulw11
In Swift you can use :
在 Swift 中,您可以使用:
UIView.performWithoutAnimation {
self.someButtonButton.setTitle(newTitle, forState: .normal)
self.someButtonButton.layoutIfNeeded()
}
回答by Christos Chadjikyriacou
Change button type to custom form interface builder.
将按钮类型更改为自定义表单界面构建器。


This worked for me.
这对我有用。
回答by shede333
Please note :
请注意 :
when "buttonType" of _button is "UIButtonTypeSystem", below code is invalid:
当_button的“ buttonType”为“UIButtonTypeSystem”时,以下代码无效:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
when "buttonType" of _button is "UIButtonTypeCustom", above code is valid.
当_button的“ buttonType”为“UIButtonTypeCustom”时,以上代码有效。
回答by Norbert
Starting in iOS 7.1 the only solution that worked for me was initializing the button with type UIButtonTypeCustom.
从 iOS 7.1 开始,唯一对我有用的解决方案是使用 type 初始化按钮UIButtonTypeCustom。
回答by dubenko
so i find worked solution:
所以我找到了可行的解决方案:
_logoutButton.titleLabel.text = NSLocalizedString(@"Logout",);
[_logoutButton setTitle:_logoutButton.titleLabel.text forState:UIControlStateNormal];
in first we change title for button, then resize button for this title
首先我们更改按钮的标题,然后调整此标题的按钮大小
回答by arunjos007
Set the button type to UIButtonTypeCustom and it'll stop flashing
将按钮类型设置为 UIButtonTypeCustom 它将停止闪烁
回答by Xhacker Liu
I've made a Swift extension to do this:
我做了一个 Swift 扩展来做到这一点:
extension UIButton {
func setTitleWithoutAnimation(title: String?) {
UIView.setAnimationsEnabled(false)
setTitle(title, forState: .Normal)
layoutIfNeeded()
UIView.setAnimationsEnabled(true)
}
}
Works for me on iOS 8 and 9, with UIButtonTypeSystem.
在 iOS 8 和 9 上对我来说有效,使用UIButtonTypeSystem.
(The code is for Swift 2, Swift 3 and Objective-C should be similar)
(代码适用于 Swift 2,Swift 3 和 Objective-C 应该类似)
回答by Carter Randall
Swift 5
斯威夫特 5
myButton.titleLabel?.text = "title"
myButton.setTitle("title", for: .normal)

