简体   繁体   English

如何使用Auto Layout在Interface Builder中创建的现有视图中添加视图?

[英]How to add views in an existing view made in Interface Builder with Auto Layout?

I have a created the following auto layout in Interface Builder: 我在Interface Builder中创建了以下自动布局:

在此处输入图片说明

As you can see I didn't give any fix size to the buttons. 如您所见,我没有为按钮提供任何固定大小。 I would like to add two button programmatically to get to this result: 我想以编程方式添加两个按钮以获得以下结果:

在此处输入图片说明

Adding the constraints programmatically I know how to do that, at least I know the syntax. 以编程方式添加约束,我知道该怎么做,至少我知道语法。

My problem is when to create those buttons? 我的问题是何时创建这些按钮?

I create the width constraint based on the width of the button 4. If I do it in viewDidLoad (if I'm not wrong), the auto layout hasn't been set yet so the width (and height) will be wrong. 我基于按钮4的宽度创建宽度约束。如果在viewDidLoad中执行宽度约束(如果我没有记错的话),则尚未设置自动布局,因此宽度(和高度)将是错误的。

I thought to do it in viewDidLayoutSubviews but as it's called multiple times when loading the viewController, I get multiple buttons stacked on each other and when I go to landscape more buttons are added.. 我本想在viewDidLayoutSubviews中执行此操作,但是由于在加载viewController时多次调用它,所以我将多个按钮堆叠在一起,而当我进入横向时,会添加更多按钮。

When should I create those button to have the right sizes? 我应何时创建这些按钮以具有合适的尺寸?

You can create the constraints programmatically in viewDidLoad . 您可以在viewDidLoad以编程方式创建约束。 If you made an IBOutlet for the buttons, then you can access them and get the size like so: 如果为按钮制作了IBOutlet ,则可以访问它们并获得如下尺寸:

self.myButton.frame.size.height;

You can use the Autolayout Constraints tool, to make this process easier. 您可以使用“自动布局约束”工具来简化此过程。

Auto layout is about rules that hold at all times, not (primarily) about frame sizes at any one moment. 自动布局是关于始终保持规则的规则,而不是(主要)关于任何时刻的帧大小。

You should not care about getting the frame of button 4 when you set up the constraints for buttons 5 and 6. The constraint that you add for buttons 5 and 6 should refer to button 4's width attribute, not its current width in points. 设置按钮5和6的约束时,您不必关心获取按钮4的框架。为按钮5和6添加的约束应参考按钮4的width属性,而不是其当前的宽度(以磅为单位)。 That is, you could create a constraint like this: 也就是说,您可以创建如下约束:

NSLayoutConstraint* constraint = [NSLayoutConstraint constraintWithItem:button5 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:button4 attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
constraint.active = YES; // OR: [button5.superview addConstraint:constraint]

That's a constraint that will keep button 5's width the same as button 4's width, even as button 4's width changes. 这是一个约束,即使按钮4的宽度发生变化,也将使按钮5的宽度与按钮4的宽度相同。 You would do the same for height, and for button 6. Etc. 您将对高度和按钮6进行相同的操作。

Put another way, the constraints you create at runtime should be similar to those you would create in IB if you were doing this at design time. 换句话说,如果在设计时执行此约束,则在运行时创建的约束应类似于在IB中创建的约束。 It doesn't look to me like you've created explicit, fixed height and width constraints on button 4. You've created relative constraints relating its height and width to other views. 在我看来,您似乎并没有在按钮4上创建明确的,固定的高度和宽度约束。您已经创建了将其高度和宽度与其他视图相关联的相对约束。

One thing you will have to do: since buttons 2 and 4 have trailing space constraints to the container (or its margins), you will need to remove those constraints when you add buttons 5 and 6. Buttons 2 and 4 would have to have trailing constraints to buttons 5 and 6, respectively, and buttons 5 and 6 would have to have trailing constraints to the container. 您将要做的一件事:由于按钮2和4对容器(或其边缘)具有尾随的空间约束,因此在添加按钮5和6时需要删除这些约束。按钮2和4必须具有尾随的空间。分别对按钮5和6进行约束,而按钮5和6必须对容器具有尾随约束。 Actually, you should simplify by getting rid of button 4's trailing constraint to the container and replacing it with a trailing alignment constraint to button 2. Likewise, button 6's trailing edge should be aligned with button 5's, not spaced from the superview's. 实际上,您应该简化操作,方法是摆脱对容器的按钮4的尾随约束,并用对按钮2的尾随对齐约束替换它。同样,按钮6的尾随边缘应与按钮5的尾部对齐,且与超级视图之间的距离不要隔开。 That way, you only have to remove one constraint (button 2's trailing to superview) and add one (button 5's trailing to superview). 这样,您只需删除一个约束(按钮2的尾部到超级视图)并添加一个约束(按钮5的尾部到超级视图)。

暂无
暂无

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

相关问题 在具有复杂视图的Interface Builder中使用自动布局 - Using Auto Layout in Interface Builder with complex views 界面生成器自动将UIImageView添加到我的视图中 - Interface Builder Auto Add UIImageView to my View 如何将使用Interface Builder创建的视图显示为UIPopoverController? - How to display a view made with Interface Builder as a UIPopoverController? 具有自动布局的界面构建器中的iOS滚动视图未滚动 - iOS scroll view in interface builder with auto layout is not scrolling 如何在Interface Builder中使用自动布局创建键盘附件视图? - How do I create a keyboard accessory view with Auto Layout in Interface Builder? 有没有办法在Interface Builder中为Auto Layout Constraints添加标识符? - Is there a way to add an identifier to Auto Layout Constraints in Interface Builder? 是否可以在Interface Builder中使用“自动布局”来放置视图,以便如果标签为空,则其他视图将不会保留该空间? - Is it possible to position views with Auto Layout in Interface Builder so that if a label is empty the other views will not keep the space? 使用乘数自动布局,在xcode 8的界面生成器中为不同的屏幕尺寸创建自动调整大小的视图 - creating auto resizeable views for different screen size in interface builder of xcode 8 using auto layout using multipliers 从代码切换到在界面生成器中创建的视图 - Switching to a view made in interface builder from code 自动布局如何在具有3个相等宽度视图的视图中隐藏1个视图 - Auto layout how to hide 1 view in a view with 3 equal width views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM