How to make an NSControl (e.g., NSTokenField) ignore mouse events

℡╲_俬逩灬. 提交于 2020-01-06 03:49:25

问题


Specifically, I'd like to make an NSTokenField ignore mouse events because I'm using it in a NSTableCellView just to display data in a tokenized way without allowing any editing.

Setting the token field's enabled = NO works, except that it greys out the tokens and makes it hard to read the text.

Setting the token field's editable = NO is pretty close to what I want—it prevents editing while preserving the token field's look—except that when I mouse over the tokens, they light up. If I could just prevent that, I'd be in business.

I suspect I need to subclass something and override some NSResponder methods, but not quite sure what to do. I tried subclassing NSTokenField and overriding mouseEntered: and mouseMoved: to do nothing, but that didn't work either.


回答1:


After trying a lot of stuff, I finally got this to work based on Iulius Cæsar's suggestion.

The trick was to subclass NSTextField and override trackingAreas:

- (NSArray *)trackingAreas
{
    return [NSArray array];
}

Simply deleting the field's tracking areas when creating it wasn't quite enough, because the field was in a scroll view and sometimes the tracking areas would be re-created.



来源:https://stackoverflow.com/questions/9876713/how-to-make-an-nscontrol-e-g-nstokenfield-ignore-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!