我有一个要求,我尝试在 View 底部水平放置 3 个按钮,没有任何间隙。我已经附上了我需要如何显示的屏幕截图和另一个显示它当前如何显示的屏幕截图。
我正在以编程方式使用以下约束来设置它
NSDictionary *views = NSDictionaryOfVariableBindings(btnCreateAccount,btnForgotuserid,btnForgotPassword);
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:btnCreateAccount attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[btnCreateAccount][btnForgotuserid(==btnCreateAccount)][btnForgotPassword(==btnCreateAccount)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:views]];
请帮我解决这个问题
编辑:在 iOS 7 中,查看屏幕截图
谢谢, 维诺德。
最佳答案
我试过你的代码,约束似乎工作正常。问题可能出在其他地方。
这是我试过的代码,以编程方式创建了所有按钮:
UIButton *b1 = [[UIButton alloc] init];
UIButton *b2 = [[UIButton alloc] init];
UIButton *b3 = [[UIButton alloc] init];
for (UIButton *b in @[b1, b2, b3]) {
[b setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:b];
[b.layer setBorderWidth:1];
}
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:b1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[b1]-0-[b2(==b1)]-0-[b3(==b1)]-0-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{ @"b1":b1, @"b2":b2, @"b3":b3 }]];
确保您在按钮上调用 setTranslatesAutoresizingMaskIntoConstraints:NO
。如果您在 Storyboard 中创建它们,则需要删除添加到那里的隐式约束。
如果您需要更多帮助,请告诉我进展情况。
关于ios - 如何使用自动布局水平设置三个按钮而没有间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29478320/