从另一个子视图的内部和下方添加子视图

时间:2014-06-10 20:48:28

标签: ios objective-c xcode cocoa uiimageview

在iOS应用中,我在主视图中添加了一个子视图:

[self.view addSubview:firstUIImageSubview];

然而在子视图类firstUIImageSubview中,我正在创建并添加另一个subView(secondUIImageSubview),我希望将其放在第一个子视图下面:

[self insertSubview:secondUIImageSubview belowSubview:self];

我的问题是第二个子视图显示在第一个子视图上方,当我想在下面显示它时。怎么可能实现这一目标?感谢。

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

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

答案 1 :(得分:0)

使用insertSubview时:belowSubview:它将子视图放在特定对象管理的其他子视图中。

[self insertSubview:secondUIImageSubview belowSubview:self];

没有多大意义。虽然self是UIView(或子类),但它仍然不应该将自己作为子视图来管理。因此

[self insertSubview:secondUIImageSubview belowSubview:firstUIImageSubview];

可能就是你想要的。但请记住,这只会将第二个UIImageSubview放在firstUIImageSubview的Z-Index(它在屏幕上的深度)方面。如果你希望它被物理地放置在firstUIImageSubview(IE它的XY坐标)下面,那么你需要使用子视图的框架设置它的位置或者设置它的原点(通过操纵它'例如,中心或锚点。

相关问题