子视图出现在超级视图layer.border下面?

Ser*_*nce 31 iphone cocoa-touch calayer uiview ios

我有一个UIView我用以下方式定义它的边框:

self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
Run Code Online (Sandbox Code Playgroud)

我附加了一个子视图UIView,当我将子视图移到边框上时,它会在它下面.这是预期的行为吗?反正有没有使子视图在它上面?

Guo*_*uan 41

根据Apple规范:It is composited above the receiver’s contents and sublayers.

因此,边框将始终位于所有子视图之上,即使您将子视图放在前面,依此类推.

所以我做了一个背景视图来伪造边框.

例如:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;

UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];

UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];

[backgroundView addSubview:bView];

[self.window addSubview:backgroundView];
Run Code Online (Sandbox Code Playgroud)

和效果:

在此输入图像描述