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