An NSMenuItem's view (instance of an NSView subclass) isn't highlighting on hover

爱⌒轻易说出口 提交于 2019-12-22 09:53:16

问题


I need to use a custom NSView subclass to draw some content, but it isn't drawing as highlighted when the user hovers and it doesn't dismiss the NSMenu when the user clicks on it. Any ideas?

Edit

So using -drawRect: and [[self enclosingMenuItem] isHighlighted] I'm able to tell whether or not I need to draw the view as highlighted and am given the chance to do so. All I have to figure out is how to do that.


回答1:


Maybe you should try it this way:

#define menuItem ([self enclosingMenuItem])

- (void) drawRect: (NSRect) rect {
    BOOL isHighlighted = [menuItem isHighlighted];
    if (isHighlighted) {
        [[NSColor selectedMenuItemColor] set];
        [NSBezierPath fillRect:rect];
    } else {
        [super drawRect: rect];
    }
}



回答2:


I'm not sure if I understood your question. I think you mean the following: The Menu opened and all your drawings stopped drawing. I think this is because the opened NSMenu stopps the UI' NSRunLoop its thread. One of both. You should try to do your drawing thread-safe in an other thread.



来源:https://stackoverflow.com/questions/2917713/an-nsmenuitems-view-instance-of-an-nsview-subclass-isnt-highlighting-on-hove

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