更改高亮显示的NSButton的背景颜色 [英] Change NSButton's background color on highlight

查看:425
本文介绍了更改高亮显示的NSButton的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSButton,我希望它的突出显示背景颜色与不突出显示的背景颜色不同(透明,未突出显示,如果有区别的话).

I have an NSButton which I want to have a different background color when it's highlighted than when it's not (transparent on not highlighted, if that makes any difference).

目前,我有以下代码

[view setWantsLayer:YES];

NSButton* button = [[NSButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[button setBordered:FALSE];
[(NSButtonCell*)[button cell] setHighlightsBy:NSChangeBackgroundCellMask];

[view addSubview:button];

这将在单击时将背景更改为默认的窗口背景颜色.如果我删除NSChangeBackgroundCellMask,背景就会消失.

This will change the background to the default window background color on click. If I remove NSChangeBackgroundCellMask the background goes away.

有没有一种简便的方法可以让背景具有不同的颜色,或者这是否要求我将NSButton子类化?

Is there an easy way I can have a different color for the background, or does this require me to subclass NSButton?

推荐答案

最后,我通过将NSButtonCell子类化并覆盖

In the end, I solved it by subclassing NSButtonCell and overwriting

- (void) highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView*)controlView

进行所需的更改(根据flag更改controlView.layer.backgroundColor)

to make the changes I wanted (changing controlView.layer.backgroundColor based on flag)

这篇关于更改高亮显示的NSButton的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆