How to prevent UIButton from flashing when updating the title

前端 未结 9 1198
旧巷少年郎
旧巷少年郎 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: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

提交回复
热议问题