简体   繁体   中英

Change contentTintColor on mouseDown for NSButton

I have a custom button that subclasses NSButton. I want to change the content tint color when the button is in pressed state. This is what I have:

open override func mouseDown(with event: NSEvent) {
    // update contentTintColor
    contentTintColor = contentTintColorPressed
    // call super to inherit the click action
    super.mouseDown(with: event)
    // for some reason mouseUp doesn't trigger if I call super, so I have to override and manually call mouseUp 
    self.mouseUp(with: event)
}

The result of this is the content tint color becomes the backgroundColor, so the button content is invisible. Why is that the contentTintColor updates only when I drag my cursor outside of the button? demo

Programmatically you can change like this:

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Change button text color when tapping is on hold.
    button.setTitleColor(UIColor.red, for: .highlighted)
}

If you have custom UIButton class, use like this:

self.setTitleColor(UIColor.red, for: .highlighted)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM