简体   繁体   English

在另一个子视图的内部和下面添加一个子视图

[英]Add a subview from within and below another subview

In an iOS app, I am adding a subview to my main view with: 在iOS应用中,我通过以下方式在主视图中添加了一个子视图:

[self.view addSubview:firstUIImageSubview];

However in the subview class firstUIImageSubview, I am creating and adding another subView (secondUIImageSubview) that I would like to put below the first subview with: 但是,在子视图类firstUIImageSubview中,我创建并添加了另一个要放置在第一个子视图下面的子视图(secondUIImageSubview):

[self insertSubview:secondUIImageSubview belowSubview:self];

My problem is that the second subview is displayed above the first subview when I want to have it below. 我的问题是,当我要在下面时,第二个子视图显示在第一个子视图的上方。 How is it possible to achieve that ? 如何实现呢? Thanks. 谢谢。

这应该可以解决问题。

[self.superview insertSubview:secondUIImageSubview atIndex:0];

When you use insertSubview:belowSubview: it places the subview in regards to other subviews that particular object manages. 当您使用insertSubview:belowSubview时:它将子视图与特定对象管理的其他子视图相关。

[self insertSubview:secondUIImageSubview belowSubview:self];

Doesn't make much sense. 没什么意义。 Although self is a UIView (or a subclass) it still should never manage itself as a subview. 尽管self是一个UIView(或子类),但它仍不应将自身作为子视图进行管理。 Therefore 因此

[self insertSubview:secondUIImageSubview belowSubview:firstUIImageSubview];

is probably what you want. 可能就是您想要的。 But remember this will only place the secondUIImageSubview below firstUIImageSubview in terms of its Z-Index (it's depth on the screen). 但是请记住,这只会将第二UIImageSubview就其Z索引(在屏幕上的深度)而言放置在firstUIImageSubview之下。 If you want it to be physically placed below firstUIImageSubview (IE it's XY coordinate) then you need to set it's position using subview's frame or setting its origin instead (by manipulating it's center or anchor points for instance). 如果要将其物理放置在firstUIImageSubview(即XY坐标)下方,则需要使用子视图的框架或设置其原点来设置其位置(例如,通过操纵其中心或锚点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM