子视图出现在父视图 layer.border 下? [英] Subview appears underneath superviews layer.border?

查看:27
本文介绍了子视图出现在父视图 layer.border 下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIView in which I define it's border in the following manner:

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

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

I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?

推荐答案

根据Apple 规范:它被合成在接收者的内容和子层之上.

因此,边框将始终高于所有子视图,即使您将子视图放在前面等等.

So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.

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

So I make a background view to fake the border.

例如:

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

和效果:

这篇关于子视图出现在父视图 layer.border 下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆