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:
[self setTitle:text fo
I've found that this workaround works with UIButtonTypeSystem as well but will only work if the button is enabled for some reason.
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
So you'll have to add these if you need the button to be disabled when setting its title.
[UIView setAnimationsEnabled:NO];
_button.enabled = YES;
[_button setTitle:@"title" forState:UIControlStateNormal];
_button.enabled = NO;
[UIView setAnimationsEnabled:YES];
(iOS 7, Xcode 5)
Combining above great answers results in following workaround for 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];
}
You can actually set the title outside of an animation block, just be sure to call layoutIfNeeded()
inside a performWithoutAnimation:
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()
}
If you have a bunch of buttons, consider just calling layoutIfNeeded()
on the super view:
button1.setTitle("abc", forState: .Normal)
button2.setTitle("abc", forState: .Normal)
button3.setTitle("abc", forState: .Normal)
UIView.performWithoutAnimation {
self.view.layoutIfNeeded()
}
UIButton with system
type has implicit animation on setTitle(_:for:)
. You can fix it two different ways:
custom
, either from code or Interface Builder:let button = UIButton(type: .custom)
UIView.performWithoutAnimation {
button.setTitle(title, for: .normal)
button.layoutIfNeeded()
}
You can remove the animations from from the title label's layer:
[[[theButton titleLabel] layer] removeAllAnimations];
Set the button type to UIButtonTypeCustom and it'll stop flashing