带有图层的NSView将绘制在所有内容上

1da*_*ake 3 cocoa nsview

我有一个NSView,我更改了一些属性,使其成为一个圆角矩形,灰色,并将其添加到我的视图(后面的一切)

float gray = 60.0f/255.0f;
NSView* background = [[NSView alloc] initWithFrame: self.iconContainer.frame];
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(gray, gray,gray, 1)]; //RGB plus Alpha Channel
[viewLayer setCornerRadius:5.0f];
[background setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
[background setLayer:viewLayer];  

// Place view behind all other views
[self.iconContainer addSubview:background positioned:NSWindowBelow relativeTo:nil];
Run Code Online (Sandbox Code Playgroud)

然而,无论我尝试什么,该特定视图都被绘制在其他一切之上.

Mac*_*ade 7

通常,此行为是由在子视图上使用Core Animation图层引起的,而superview没有.

在superview上启用Core Animation图层应该可以解决问题,因为任何子视图都将使用Cora Animation图层绘制,使绘图过程遵循子视图的顺序.