如何在constraintsWithVisualFormat中一起添加水平和垂直视觉格式?只有水平。我想添加V:| -50- [leftButton]和V:| -50- [rightButton]。怎么做?创建另一个NSLayoutConstraint?
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[leftButton setTitle:@"Left" forState:UIControlStateNormal];
[leftButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:leftButton];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rightButton setTitle:@"Right" forState:UIControlStateNormal];
[rightButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:rightButton];
NSArray *layoutConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[leftButton(>=80)]-50-[rightButton(>=80)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(leftButton, rightButton)];
[self.view addConstraints:layoutConstraints];
}
最佳答案
您可以根据需要使用可视格式创建尽可能多的独立约束集。您不能在同一字符串中混合水平和垂直约束,但是没有什么阻止您创建:
H:|-[view]-|
其次是
V:|-[view]-|
可以将每个VFL语句视为在 super 视图的单行或单列中表示布局。