ios - 如何使用自动布局水平设置三个按钮而没有间隙

标签 ios iphone uibutton xcode6 autolayout

我有一个要求,我尝试在 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]];

required Actual

请帮我解决这个问题

编辑:在 iOS 7 中,查看屏幕截图

enter image description here

谢谢, 维诺德。

最佳答案

我试过你的代码,约束似乎工作正常。问题可能出在其他地方。

这是我试过的代码,以编程方式创建了所有按钮:

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/

相关文章:

iphone - 无法将栏按钮添加到 UIToolBar

ios - 通过蓝牙将 iPhone 连接到 Arduino

iphone - 哪个 JSON 库用于 iPhone 上的数据

iphone - 用于切换 wifi 和 3G 的专用 API

swift - 多个按钮可实现一个操作,无需 IB

ios - setAttributedText 不适用于 UIButton?

ios - UIView.transition 在模拟器中淡出但在设备上不淡出

c# - 在 Xamarin.iOS 中隐藏和显示控件

ios - 我的 UIButton 无法在触摸时禁用?

iphone - 当 iphone/设备进入休眠或锁定时继续播放音频