exs*_*lto 197 objective-c uibutton uikit ios7 swift
在iOS 7中,我的UIButton游戏在错误的时间进行动画制作 - 迟到.这个问题不会出现在iOS 6上.我只是使用:
[self setTitle:text forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
我希望这种情况立即发生,没有空白框架.这种眨眼特别令人分心,并引起人们对其他动画的注意.
Sno*_*man 249
使用该performWithoutAnimation:方法,然后立即强制布局,而不是以后.
[UIView performWithoutAnimation:^{
[self.myButton setTitle:text forState:UIControlStateNormal];
[self.myButton layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)
Jac*_*b K 158
这适用于自定义按钮:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
对于系统按钮,您需要在重新启用动画之前添加它(谢谢@Klaas):
[_button layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
Pau*_*w11 68
在Swift中你可以使用:
UIView.performWithoutAnimation {
self.someButtonButton.setTitle(newTitle, forState: .normal)
self.someButtonButton.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
小智 60
请注意 :
当_button的 " buttonType "是"UIButtonTypeSystem"时,下面的代码无效:
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
当_button的 " buttonType "为"UIButtonTypeCustom"时,上面的代码有效.
dub*_*nko 18
所以我找到了解决方案:
_logoutButton.titleLabel.text = NSLocalizedString(@"Logout",);
[_logoutButton setTitle:_logoutButton.titleLabel.text forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
首先我们更改按钮的标题,然后调整此标题的大小按钮
Xha*_*Liu 12
我做了一个Swift扩展来做到这一点:
extension UIButton {
func setTitleWithoutAnimation(title: String?) {
UIView.setAnimationsEnabled(false)
setTitle(title, forState: .Normal)
layoutIfNeeded()
UIView.setAnimationsEnabled(true)
}
}
Run Code Online (Sandbox Code Playgroud)
在iOS 8和9上适用于我UIButtonTypeSystem.
(代码适用于Swift 2,Swift 3和Objective-C应该类似)
sgl*_*l0v 11
具有system类型的UIButton 在 上具有隐式动画setTitle(_:for:)。您可以通过两种不同的方式修复它:
custom从代码或界面生成器中将按钮类型设置为:let button = UIButton(type: .custom)
Run Code Online (Sandbox Code Playgroud)
UIView.performWithoutAnimation {
button.setTitle(title, for: .normal)
button.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您只需创建自定义按钮,它就会在更改标题时停止动画.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"the title" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
你也可以在故事板复选框中选择:在故事板中选择按钮 - >选择属性检查器(左侧第四个) - >在"类型"下拉菜单中,选择"自定义"而不是可能选中的"系统" .
祝好运!
通常,将按钮类型简单地设置为“自定义”对我有用,但是由于其他原因,我需要继承UIButton并将按钮类型设置回默认值(“系统”),因此闪烁再次出现。
UIView.setAnimationsEnabled(false)更改标题之前先进行设置,然后再更改为true,无论我是否打电话,都无法避免闪烁self.layoutIfNeeded()。
这,并且仅按照以下确切顺序,才对我适用于iOS 9和10 beta:
1)为UIButton创建一个子类(也不要忘记在Storyboard中为按钮设置自定义类)。
2)覆盖setTitle:forState:如下:
override func setTitle(title: String?, forState state: UIControlState) {
UIView.performWithoutAnimation({
super.setTitle(title, forState: state)
self.layoutIfNeeded()
})
}
Run Code Online (Sandbox Code Playgroud)
在Interface Builder中,您可以将按钮类型保留为System,而无需将其更改为Custom Type即可使用此方法。
我希望这对其他人有帮助,我已经通过烦人的闪烁按钮苦苦挣扎了很长时间,希望可以避免对其他人使用;)
小智 6
myButton.titleLabel?.text = "title"
myButton.setTitle("title", for: .normal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46943 次 |
| 最近记录: |