gpt4 book ai didi

iphone - subview 出现在 superviews layer.border 下面?

转载 作者:IT王子 更新时间:2023-10-29 07:47:36 26 4
gpt4 key购买 nike

我有一个 UIView,我在其中按以下方式定义它的边框:

self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;

我将一个 subview 附加到此 UIView,当我将 subview 移到边框上方时,它会在边框下方。这是预期的行为吗?有没有办法让 subview 位于它之上?

最佳答案

根据Apple specification : 它合成在接收者的内容和子层之上。

因此,边框将始终位于所有 subview 之上,即使您将 subview 置于最前面等等。

所以我制作了一个背景 View 来伪造边框。

例如:

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];

及效果:

enter image description here

关于iphone - subview 出现在 superviews layer.border 下面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com