在以编程方式创建的 UIView 层次结构之上添加子视图

发布于 2025-01-04 11:39:55 字数 282 浏览 0 评论 0原文

我有一个 UIView 子类,它以编程方式创建并添加许多子视图。然而,我想让该类的用户能够使用 addSubview 在它(及其子视图)上放置新的子视图,最好也从 Interface Builder 中放置新的子视图。

最好的方法是什么?

现在,我想仅使用 addSubview 以编程方式添加视图是可以接受的,但我也不确定如何解决这个问题。

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

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?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2025-01-11 11:39:55

只需将通用 UIView 拖到 Interface Builder 中的笔尖中,并将其类设置为您的自定义子类即可。您仍然可以使用普通的旧 UIView 执行所有操作,包括拖放子视图。

无法在 Interface Builder 中看到自定义子视图的实际外观;它只是一个灰色矩形。你可以(我认为)在 Xcode 3 中使用 IB 插件来完成此操作,但是,至少目前,Xcode 4 不支持此类插件。

编辑:

为了解决OP的后续问题,您需要以编程方式管理z顺序。也许最简单的事情就是让你的代码基本上保持原样,而不是做类似的事情:(

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

正如你所指出的,这很可能将子视图分层在用户的子视图之上)

做类似的事情:

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] 但我认为在快速浏览代码时添加并移动到后面会更清晰一些。

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

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.

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.

EDIT:

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)

Do something like:

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文