NSTextField click-through?

2019-02-22 02:36发布

I have a static NSTextField that overlays a large error message in my OS X app. I'm trying to get it to allow the user to click controls beneath it.

In IB I've unchecked "enabled" and I've checked "Refuses First Responder"

I've also done it in code because that wasn't working:

[largeErrorText setEnabled:NO];
[largeErrorText setRefusesFirstResponder:YES];

Still, it is getting in the way of interacting with the objects below it. Any ideas what else it might be?

2条回答
2楼-- · 2019-02-22 02:59

I assume you are describing a scenario like the following image shows:

enter image description here

The inner red rectangle is the frame outline of the NSTextField label, and you're saying that even though you've disabled the text field and set refuses first responder, your clicks do not go through to the NSButton?

This design scenario describes a condition called "Overlapping sibling views". I would generally try to avoid this if at all possible. If you can't, you can get the desired behavior by making sure that the NSTextField label is "behind" all of the other UI objects that you want to be able to interact with. You can do that by selecting the label and choosing Editor > Arrange > Send to Back. That will assure that the button is in front of the text field so that it can properly intercept mouse events.

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-22 03:06

The only way I have found to make an object transparent to the click is to subclass that object (in your case the NSTextField) and override the hitTest method returning nil. This way that NSTextField will not respond to the click so the NSView below will respond to the click.

- (NSView*)hitTest:(NSPoint)aPoint
{
    return nil;
}
查看更多
登录 后发表回答