简体   繁体   English

如何使用自动布局水平设置三个按钮而无间隙

[英]How to set three buttons horizontally without gap using autolayout

I have a requirement where , i try to set 3 buttons at the bottom of the view horizontally placed without any gap . 我有一个要求,我尝试在视图底部将3个按钮设置为水平放置,没有任何间隙。 I have attached a screen shot of how i need to show and another showing how it displays currently. 我已附上我需要显示的屏幕截图,以及另一个显示当前显示方式的屏幕截图。

i am using the following constraint programatically to set this 我正在以编程方式使用以下约束来设置此约束

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]];

需要实际

Please help me in solving this issue 请帮我解决这个问题

EDIT : In iOS 7 , see the screen shot 编辑:在iOS 7中,请看屏幕截图

在此处输入图片说明

Thanks, Vinod. 谢谢,维诺德。

I have tried your code and the constraints seem to work fine. 我已经尝试过您的代码,并且约束似乎工作正常。 The problem might be somewhere else. 问题可能出在其他地方。

This is the code I have tried, created all buttons programmatically: 这是我尝试过的代码,以编程方式创建了所有按钮:

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 }]];

Make sure you are calling setTranslatesAutoresizingMaskIntoConstraints:NO on your buttons. 确保您在按钮上调用setTranslatesAutoresizingMaskIntoConstraints:NO If you create them in the storyboard, you need to remove the implicit constraints that are added there. 如果在情节提要中创建它们,则需要删除在其中添加的隐式约束。

Let me know how this goes and if you need more help. 让我知道如何进行以及是否需要更多帮助。

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

相关问题 使用自动布局以编程方式在视图中水平对齐按钮 - Horizontally align buttons in a view using autolayout programatically 如何以编程方式设置 UIScrollView 以使用 autoLayout 水平滚动 - How to set up UIScrollView programatically to scroll horizontally using autoLayout 如何在ios中使用自动布局将三个按钮并排放置在中间位置? - How to place three buttons in centre side by side with spacing using autolayout in ios? Autolayout约束以水平设置标签 - Autolayout constraints to set labels horizontally 使用Autolayout水平对齐5个按钮的最佳方法是什么 - What is the best approach for horizontally aligning 5 buttons with Autolayout Xcode 5:如何在IB中使用自动布局水平对齐2个对象? - Xcode 5: How to align 2 object horizontally using Autolayout in IB? 使用自动布局在屏幕上均匀地展开按钮。 它们之间的间距不相等 - Evenly spread buttons horizontally across screen using autolayout. Not equal spacing between them 如何在Swift中自动布局按钮 - How to autolayout buttons in Swift 如何在iOS中为多个动态按钮和标签设置自动布局? - how Set autolayout for multiple dynamic buttons and labells in iOS? 如何在自动布局中的视图下设置不同数量的按钮的宽度 - How to set width of different amount of buttons underneath a view in autolayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM