在以编程方式创建的UIView层次结构上添加子视图 [英] Adding subviews on top of a programmatically-created UIView hierarchy

查看:112
本文介绍了在以编程方式创建的UIView层次结构上添加子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView子类,它以编程方式创建并添加了许多子视图。但是,我想让类的用户能够使用addSubview在其上放置新的子视图(及其子视图),理想情况下也可以在Interface Builder中。

I have a UIView subclass that programmatically creates and adds a number of subviews. However, I want to give users of the class the ability to place new subviews over it (and its subviews) using addSubview and ideally from within Interface Builder as well.

什么这是最好的方法吗?

What is the best way to do this?

现在,我认为只使用addSubview以编程方式添加视图是可以接受的,但我不确定我会怎么做关于这个。

Now, I suppose it would be acceptable to only add the views programmatically using addSubview, but I'm not sure how I would go about this either.

注意:我尝试使用界面构建器添加其他子视图,但是一旦我上面提到的自定义UIView子类创建了其他视图,它们就会被隐藏以编程方式,大概是因为它们是最后添加的。

NOTE: I've tried adding additional subviews using interface builder, but they're hidden once the custom UIView subclass I refered to above creates its other views programmatically, presumably since they're added last.

推荐答案

只需将通用UIView拖入Interface Builder中的nib并设置其类到你的自定义子类。您仍然可以使用普通的旧UIView做所有事情,包括拖放子视图。

Just drag a generic UIView into a nib in Interface Builder and set its class to your custom subclass. You can still do everything you could with a plain old UIView, including drag-dropping subviews.

不会能够看到什么自定义子视图实际上在Interface Builder中看起来像;它只是一个灰色矩形。您可以(我认为)在Xcode 3中使用IB插件完成此操作,但是,至少目前,Xcode 4不支持此类插件。

You won't be able to see what the custom subview actually looks like in Interface Builder; it will just be a gray rectangle. You could have (I think) done this in Xcode 3 with IB plugins, but, at least for the time being, Xcode 4 does not support such plugins.

编辑:

要解决OP的后续行动,您需要以编程方式管理z排序。可能最容易做的就是保持代码大部分原样,但不要做类似的事情:

To address the OP's follow-up, you need to manage z-ordering programmatically. Probably the easiest thing to do is to leave your code largely as-is, but instead of doing something like:

UIView *mySubview1 = ...;
UIView *mySubview2 = ...;
UIView *mysubview3 = ...;
[self addSubview:mySubview1];
[self addSubview:mySubview2];
[self addsubview:mySubview3];

(正如您所指出的,可能会将子视图放在用户子视图之上)

(which, as you noted, may well layer the subviews on top of the user's subviews)

执行以下操作:

UIView *container = [[UIView alloc] initWithFrame:[self bounds]];
UIView *mySubview1 = ...;
UIView *mySubview2 = ...;
UIView *mysubview3 = ...;
[container addSubview:mySubview1];
[container addSubview:mySubview2];
[container addsubview:mySubview3];
[self addSubview:container];
[self sendSubviewToBack:container];
[container release];

您也可以使用[self insertSubview:container atIndex:0]但我认为添加并移动到在快速浏览代码时,背面更清晰。

You could alternatively use [self insertSubview:container atIndex:0] but I think that adding and moving to the back is a little clearer during a rapid skim of the code.

最终结果是您不必弄乱子视图的现有z排序;通过将它们放在一个易于管理的单个容器中,您可以将整个集合移动到子视图层次结构中的任何位置。

The end result is that you don't have to mess with the existing z-ordering of your subviews; by putting them in a single, easily-managed container, you can move the whole collection to wherever you would like in the subview hierarchy.

这篇关于在以编程方式创建的UIView层次结构上添加子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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