子视图显示在superviews layer.border下面? [英] Subview appears underneath superviews layer.border?

查看:297
本文介绍了子视图显示在superviews 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规格它是在

According to the Apple specification: It is composited above the receiver’s contents and sublayers.

所以,边框将永远在你所有的子视图之上,即使你把你的子视图带到前面,

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.



E.g.:

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

和效果:

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

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