How to prevent UIButton from flashing when updating the title

前端 未结 9 1026
旧巷少年郎
旧巷少年郎 2021-01-30 04:41

When I call setTitle on a UIButton, the button flashes in iOS 7. I tried setting myButton.highlighted = NO, but that didn\'t stop the button from flashing.

[myBu         


        
相关标签:
9条回答
  • 2021-01-30 05:21

    Default "setTitle" behavior is definitely hateful!

    My solution is:

    [UIView setAnimationsEnabled:NO];
    [_button layoutIfNeeded]; 
    [_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal];
    [UIView setAnimationsEnabled:YES];
    

    Also, in storyboard, under button's property I uncheck:

    • Reverses On Highlight
    • Highlighted Adjust Image

    And check:

    • Disabled Adjust Image

    Tested and working on iOS 8 too.

    Update - Swift

    I suggest you to create your custom button and override setTitle method.

    class UnflashingButton: UIButton {
       
        override func setTitle(title: String?, forState state: UIControlState) {
            UIView.setAnimationsEnabled(false)
            self.layoutIfNeeded()
            super.setTitle(title, forState: state)
            UIView.setAnimationsEnabled(true)
        }
    
    }
    
    0 讨论(0)
  • 2021-01-30 05:22

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

    0 讨论(0)
  • 2021-01-30 05:25

    Try this, this works in newer versions of IOS:

    class CustomButtonWithNoEffect : UIButton {
        override func setTitle(_ title: String?, for state: UIControlState) {
            UIView.performWithoutAnimation {
                super.setTitle(title, for: state)
                super.layoutIfNeeded()
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-30 05:32

    You can simply just make another function for UIButton for easy future usage.

    extension UIButton {
        func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
            UIView.performWithoutAnimation {
                self.setTitle(title, for: controlState)
                self.layoutIfNeeded()
            }
        }
    }
    

    That is it!

    And just set the title as you would before but replace setTitle with setTitleWithoutAnimation

    0 讨论(0)
  • 2021-01-30 05:32

    Another trick

    @IBOutlet private weak var button: UIButton! {
        didSet {
            button.setTitle(title, for: .normal)
        }
    }
    

    This works only if you know the value of the title without making any API calls. The button's title will be set before the view is loaded so you won't see the animation

    0 讨论(0)
  • 2021-01-30 05:36

    *Please note *

    when "buttonType" of _button is "UIButtonTypeSystem", below code is invalid

    [UIView setAnimationsEnabled:NO];
    [_button setTitle:@"title" forState:UIControlStateNormal];
    [UIView setAnimationsEnabled:YES];
    

    when "buttonType" of _button is "UIButtonTypeCustom", above code is valid.

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