0

我有一个子类 NSButton 的自定义按钮。我想在按钮处于按下状态时更改内容色调颜色。这就是我所拥有的:

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)
}

这样做的结果是内容的色调颜色变成了背景颜色,因此按钮内容是不可见的。为什么只有当我将光标拖到按钮外时 contentTintColor 才会更新?演示

4

1 回答 1

0

以编程方式,您可以像这样进行更改:

@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)
}

如果您有自定义 UIButton 类,请像这样使用:

self.setTitleColor(UIColor.red, for: .highlighted)
于 2020-12-24T19:37:26.173 回答