使用 iOS 6 中的自动布局在超级视图中均匀间隔大小相等的 UIView [英] Evenly Space UIViews of equal sizes in superview using Auto Layout in iOS 6
问题描述
我需要使用自动布局将一系列按钮水平均匀地放置在超级视图中.在这里,我想保持子视图的大小相同,只有子视图的中心会以它们之间有相等数量的空间的方式放置.
I am in a need of having the series of buttons to be evenly placed in superview Horizontally using Auto Layout. Here, I want to keep the sizes of the subviews same, only the center of the subviews will be placed in such a way that there is equal number of space between them.
注意:我不想设置超级视图的大小,我希望所有东西都是自动布局的.
Note: I dont want to set the Size of the superview, I want every thing to be Auto Layout-ed.
请帮忙,我被卡住了!!
Please Help, I am stuck !!
谢谢!!
推荐答案
你可以创建尽可能多的 UIView
按钮,并在视图中居中按钮,视图可以对齐背靠背,使用此代码:
You can create as many UIView
's as you have buttons, and center the buttons inside the views, the views can be aligned back to back, using this code:
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1][view2][view3]|"
options:0
metrics:nil
views:views]];
确保首先使用以下方法从超视图中删除现有约束:
Make sure you first remove existing constraints from the superview using:
[self.view removeConstraints:self.view.constraints];
并在 viewDidLoad 中关闭自动调整大小转换:
and in the viewDidLoad turn off auto resizing conversion:
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
这篇关于使用 iOS 6 中的自动布局在超级视图中均匀间隔大小相等的 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!