简体   繁体   English

使用XIB和自动布局来操纵视图层次结构

[英]Manipulate view hierarchy using XIBs and auto-layout

What I'm trying to accomplish seems pretty straight-forward, but I've been on this for an entire day now and I can't figure it out. 我试图完成的工作看起来很简单,但是我已经整整整整一天地工作了,我无法弄清楚。

I have a storyboard (which does not yet use auto-layout) with a scene that has a paged scroll view. 我有一个情节提要板(尚未使用自动布局),其场景具有分页的滚动视图。 I also have 2 XIBs that I instantiate in code. 我还有2个在代码中实例化的XIB。 The first XIB (A) has a UILabel and a generic UIView . 第一个XIB(A)具有一个UILabel和一个通用UIView The second XIB (B) has just 5 UIButton s in a + arrangement. 第二个XIB(B)只有5个UIButton +。 The ultimate goal is to load B into the generic view in A (horizontally centered and the "default value" below the label), and to make A take up a whole page in the scroll view. 最终目标是将B加载到A的通用视图中(水平居中,标签下方为“默认值”),并使A在滚动视图中占据整个页面。

When I resize the view of XIB A, the generic view's size expands and shrinks, but always stays the default value below the label. 调整XIB A的视图大小时,通用视图的大小会扩大和缩小,但始终保持默认值在标签下方。 When I resize the view of XIB B, the button arrangement stays centered (which is what I want). 当我调整XIB B的大小时,按钮排列保持居中(这就是我想要的)。 This is the behavior that I want. 这是我想要的行为。

With the following code, though, it does not work: 但是,使用以下代码将无法正常工作:

View A *viewA = [[ViewA alloc] initWithFrame:CGRectZero];
viewA.textLabel.text = @"A label with a rather long text";

ViewB *viewB = [[ViewB alloc] initWithFrame:CGRectZero];

[viewA setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewB setTranslatesAutoresizingMaskIntoConstraints:NO];

[viewA.customView addSubview:viewB]; // customView is the generic view

This gives me the following: 这给了我以下内容:

在此处输入图片说明

(The white area is the scroll view, the red area view A, the green area view B.) (白色区域是滚动视图,红色区域是A,绿色区域是B。)

I tried adding a constraint that should let view B take up the whole width of its superview: 我尝试添加一个约束,该约束应使视图B占据其超级视图的整个宽度:

[viewA.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[viewB]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(viewB)]];

But that results into this: 但这导致了:

在此处输入图片说明

What can I do to get the buttons centered in the green area? 如何使按钮居中于绿色区域? I don't care if the green area is completely covered by the red area, or if the red area is just centered over the green area. 我不在乎绿色区域是否完全被红色区域覆盖,或者红色区域是否只是在绿色区域上方居中。

You mentioned not using auto-layout in the xibs, so i would assume adding constraints in code has zero effect. 您提到没有在xib中使用自动布局,因此我假设在代码中添加约束的效果为零。 You should try using the appropriate AutoresizingMasks for views added in code. 您应该尝试对代码中添加的视图使用适当的AutoresizingMask。 Try to use Auto-Layout (correctly) in xibs unless there is a good reason not to. 除非有充分的理由,否则请尝试在xibs中正确使用“自动布局”。

An example of a Mask is below, you need to figure out which ones you need, cant really tell by your example. 下面是一个遮罩的示例,您需要弄清楚所需的遮罩,无法通过示例真正看出来。

yourView.translatesAutoresizingMaskIntoConstraints = NO;
[yourView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

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

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