How to prevent UIButton from flashing when updating the title

前端 未结 9 1032
旧巷少年郎
旧巷少年郎 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)
        }
    
    }
    

提交回复
热议问题