How to stop unwanted UIButton animation on title change?

前端 未结 24 2544
面向向阳花
面向向阳花 2020-11-28 19:07

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         


        
相关标签:
24条回答
  • 2020-11-28 19:24

    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)

    0 讨论(0)
  • 2020-11-28 19:24

    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];
    }
    
    0 讨论(0)
  • 2020-11-28 19:24

    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()
    }
    
    0 讨论(0)
  • 2020-11-28 19:25

    UIButton with system type has implicit animation on setTitle(_:for:). You can fix it two different ways:

    1. Set the button type to custom, either from code or Interface Builder:
    let button = UIButton(type: .custom)
    

    1. Disable animation from code:
    UIView.performWithoutAnimation {
        button.setTitle(title, for: .normal)
        button.layoutIfNeeded()
    }
    
    0 讨论(0)
  • 2020-11-28 19:25

    You can remove the animations from from the title label's layer:

        [[[theButton titleLabel] layer] removeAllAnimations];
    
    0 讨论(0)
  • 2020-11-28 19:29

    Set the button type to UIButtonTypeCustom and it'll stop flashing

    0 讨论(0)
提交回复
热议问题