简体   繁体   English

iOS - Interface Builder - 在同一个xib中绑定多个视图

[英]iOS - Interface Builder - bind multiple view inside the same xib

I'm trying to organize a complex xib into multiple view. 我正在尝试将复杂的xib组织成多个视图。

Suppose the following scenario: 假设以下场景:

I have the main view (the green one) that contains two subviews (the red and the yellow ones). 我有主视图(绿色视图),包含两个子视图(红色和黄色)。
Actually I can create three xib and add the subviews programmatically. 实际上我可以创建三个xib并以编程方式添加子视图。
I found a solution a bit smarter: I define two IBOutlet in the main view (green) and connect them to the subviews. 我找到了一个更聪明的解决方案:我在主视图中定义了两个IBOutlet(绿色)并将它们连接到子视图。

@property (nonatomic, weak) IBOutlet RedView * redView;
@property (nonatomic, weak) IBOutlet YellowView * yellowView;

In the main view implementation, programmatically, I add the subviews: 在主视图实现中,我以编程方式添加子视图:

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.redView.frame = self.frame;
    self.redView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.yellowView.frame = self.frame;
    self.yellowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    [self addSubview:self.redView];
    [self addSubview:self.yellowView];
}

As you can see, I need to set the current frame size, the autoresizingMask and add the subViews to the parent. 如您所见,我需要设置当前帧大小,autoresizingMask并将subViews添加到父级。

The question: 问题:

is there a nice way to perform these operations directly from the xib? 有没有一种很好的方法直接从xib执行这些操作? I want to load the xib ("loadNibNamed") and obtain the main view (green) already filled with the subviews. 我想加载xib(“loadNibNamed”)并获取已填充子视图的主视图(绿色)。

I've tried some solutions but neither works... 我尝试了一些解决方案,但都没有...

edit: 编辑:
I don't want to add the red and yellow view as subviews directly in IB. 我不想直接在IB中添加红色和黄色视图作为子视图。
The two subviews are as big as the parent and this make complex to work with Interface Builder (hide the yellow view and work on the red, then hide the red and work on the yellow...). 这两个子视图与父级一样大,这使得使用Interface Builder变得复杂(隐藏黄色视图并处理红色,然后隐藏红色并处理黄色......)。
I'd like to have separate views in the same xib but being able to "compose" them in parent-child tree... 我想在同一个xib中有单独的视图,但能够在父子树中“组合”它们...

Instead of creating the red and yellow views by dragging a view into the open space. 而不是通过将视图拖入开放空间来创建红色和黄色视图。 Drag the view onto the green view. 将视图拖动到绿色视图上。 It will then be added as a subview. 然后将其添加为子视图。

You can move your existing views only by using the list on the left. 您只能使用左侧的列表移动现有视图。 Drag the row in the list that corresponds to the red view onto the row in the list that is the green view. 将列表中与红色视图对应的行拖到列表中作为绿色视图的行上。

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

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