NSView invisible unless I override drawRect, and then it gets clipped even if setMasksToBounds is NO on superview's layer

StackOverflow https://stackoverflow.com/questions/16694384

Pregunta

Trying to add a NSView to an existing NSView, but I'm failing miserably for some strange reason. I'm doing this:

        _resizeView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 10, 10)];
    [_resizeView.layer setBorderWidth:2];
    [_resizeView.layer setBorderColor:[NSColor purpleColor].CGColor];
    [_resizeView.layer setBackgroundColor:[NSColor blackColor].CGColor];
    [_resizeView setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin | NSViewMaxYMargin];
    [self addSubview:_resizeView];

Tried removing the autoresizing mask but that still didn't work. So then I changed it from NSView to NSButton, and that magically works and the view is visible.

Created my own subclass of NSView where I'm overriding drawRect, and then it suddenly becomes visible, but only for the paths that I draw myself there (yes I'm calling super drawRect). What is going on here?

So I figured since I'm just basically doing a box with a border (background color was just for testing) I could just keep on using my subclass, but then I try to add this to the corner of my existing view, sticking out slightly, and it gets masked even if I have setMasksToBounds set to NO for the primary layer of the view that I'm adding it to.

I'm going mad here! Why is none of this working as expected?

¿Fue útil?

Solución

If you use a layer-backed view you have to call

[_resizeView setWantsLayer:YES];

And maybe

[_resizeView setNeedsDisplay:YES];

but I don't think so, because it's just an NSView.

Otros consejos

[_resizeView setNeedsDisplay:YES];  

Whenever the data or state used for drawing a view object changes, the view should be sent a setNeedsDisplay: message.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top