[英]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.