fxf*_*ure 16 iphone objective-c uibutton ios autolayout
我看了很多答案,看起来都很复杂!最近我看到了这个答案,虽然我不想把我的按钮放在视图中.
我有6个UIButton,它们都是相同的尺寸.我想将它们均匀地水平分布在我的根视图控制器的整个宽度和底部.
| |
| |
| [b1] [b2] [b3] [b4] [b5] [b6] |
________________________________________
Run Code Online (Sandbox Code Playgroud)
以编程方式实现此目的的最简单方法是什么?
ban*_*isa 20
我认为这比接受答案的链接更简单.好的,首先让我们创建一些按钮:
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.translatesAutoresizingMaskIntoConstraints = NO;
[button1 setTitle:@"Btn1" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
...为6个按钮做6次,但是你喜欢然后将它们添加到视图中:
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
[self.view addSubview:button4];
[self.view addSubview:button5];
[self.view addSubview:button6];
Run Code Online (Sandbox Code Playgroud)
将一个按钮修复到视图的底部:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
Run Code Online (Sandbox Code Playgroud)
然后告诉所有按钮宽度相等,并在宽度上均匀分布:
NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2, button3, button4, button5, button6);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button1][button2(==button1)][button3(==button1)][button4(==button1)][button5(==button1)][button6(==button1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:views]];
Run Code Online (Sandbox Code Playgroud)
结果:

| 归档时间: |
|
| 查看次数: |
12503 次 |
| 最近记录: |