![](/img/trans.png)
[英]How can I programmatically set the vertical alignment of a UIButton - iOS
[英]iOS: Vertical Alignment of UIButton in iOS
我需要为四个按钮添加垂直对齐方式,以便它应该在下面移动几个点。 下面是我的代码,现在它在水平顶部显示了四个UIButton。
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"CLOTHING" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor orangeColor];
button1.translatesAutoresizingMaskIntoConstraints = NO;
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTitle:@"ACCESSORIES" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor orangeColor];
button2.translatesAutoresizingMaskIntoConstraints = NO;
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3 setTitle:@"FOOTWEAR" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor orangeColor];
button3.translatesAutoresizingMaskIntoConstraints = NO;
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
[button4 setTitle:@"GARMENT" forState:UIControlStateNormal];
button4.backgroundColor = [UIColor orangeColor];
button4.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
[self.view addSubview:button4];
NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2,button3,button4);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[button1]-5-[button2]-5-[button3]-5-[button4]-5-|"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views];
// Find the equal width constraint and set priority to high (750)
for (NSLayoutConstraint *constraint in horizontalConstraints) {
if (constraint.firstAttribute == NSLayoutAttributeWidth) {
constraint.priority = UILayoutPriorityDefaultHigh;
}
}
[self.view addConstraints:horizontalConstraints];
您正在提供水平约束以此更改约束
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[button1]-5-[button2]-5-[button3]-5-[button4]"
options:0
metrics:nil
views:views];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.