Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSImageView disappears when the parent NSView wants layer (setWantsLayer:YES)

I have a simple custom NSView that has a label and an image. Currently the view works quite well (all the thumbs are instances of my custom view):

enter image description here

However, I want to set the opacity of these thumbnails. Setting viewInstance.layer.opacity doesn't work as the layer is nil. When I use [viewInstance setWantsLayer:YES] first somewhere, and I set the opacity, the opacity of the text changes (it is displayed correctly), but the image view disappears altogether:

enter image description here

I've tried creating a layer first, assigning it, and then calling setWantsLayer: after reading this question: How to add a CALayer to an NSView on Mac OS X but nothing changed:

CALayer *selfLayer = [[CALayer alloc] init];
[selfLayer setFrame:CGRectMake(0, 0, 100, 100)];
[self setLayer:selfLayer];
[self setWantsLayer:YES];

I've tried all combinations of creating a layer and displaying it for the image view too (just calling setWantsLayer:, creating layer and then assigning it, calling display method of it), but still nothing changes:

[self addSubview:self.imageView];
CALayer *imageLayer = [[CALayer alloc] init];
[imageLayer setFrame:CGRectMake(0, 0, 100, 100)];
[self.imageView setLayer:imageLayer];
[self.imageView setWantsLayer:YES];

I've tried adding the image view's layer as a sublayer of the main view and then displaying it:

[self.layer addSublayer:self.imageView.layer];
[self.imageView.layer display];
[self.layer display];

But still, the images won't display, whereas the text always displays correctly. When I comment out the code about assigning and wanting layers, images display again, but obviously, I can't use the layer opacity. I am on OS X Mavericks with OS X SDK 10.8. What am I doing wrong?

UPDATE: I ended up getting rid of NSImageView completely and drawing the NSImage directly into the layer by setting the layer's contents property. However, this is just another approach which solved my problem for this project, it still doesn't answer the original question.

like image 961
Can Poyrazoğlu Avatar asked Nov 11 '22 19:11

Can Poyrazoğlu


1 Answers

I ended up getting rid of NSImageView completely and drawing the NSImage directly into the layer by setting the layer's contents property. However, this is just another approach which solved my problem for this project, it still doesn't answer the original question.

like image 197
Can Poyrazoğlu Avatar answered Nov 15 '22 13:11

Can Poyrazoğlu