各行の最初:テキストに合うようにラベルのサイズを変更します。次に、ラベルとテキストフィールドの間に水平方向の間隔を置きます。
次に、すべての左端が整列するテキストフィールドに優先度の高い制約を追加します。
これは2行に対してそれを行ういくつかのコードです:
- (void)viewDidLoad{
[super viewDidLoad];
//create labels and text fields for row 1
UILabel *label0,*label1;
UITextField *textField0,*textField1;
label0 = [[UILabel alloc] init];
label0.backgroundColor=[UIColor redColor];
[self.view addSubview:label0];
textField0 = [[UITextField alloc] init];
textField0.backgroundColor=[UIColor greenColor];
[self.view addSubview:textField0];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[label0 setTranslatesAutoresizingMaskIntoConstraints:NO];
[textField0 setTranslatesAutoresizingMaskIntoConstraints:NO];
//*************************************************************
//layout row 1
NSArray *arr;
NSDictionary *dict = NSDictionaryOfVariableBindings(label0,textField0);
NSLayoutConstraint *constraint;
//label sized to fit text (default) and standard spacing between label and textField
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label0]-[textField0(100)]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
//vertical constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
//*************************************************************
//create label and textfield for row2
label1 = [[UILabel alloc] init];
label1.backgroundColor=[UIColor redColor];
[self.view addSubview:label1];
textField1 = [[UITextField alloc] init];
textField1.backgroundColor=[UIColor greenColor];
[self.view addSubview:textField1];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[textField1 setTranslatesAutoresizingMaskIntoConstraints:NO];
//*************************************************************
//layout row 2
dict = NSDictionaryOfVariableBindings(label0,textField0,label1,textField1);
//label sized to fit text (default) and standard spacing between label and textField
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label1]-[textField1(100)]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
//vertical constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]-[label1]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]-[textField1]"
options:0
metrics:nil
views:dict];
[self.view addConstraints:arr];
//*************************************************************
//line up the left edges of the text fields and make it a higher priority to override spacing
constraint = [NSLayoutConstraint constraintWithItem:textField0 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:textField1 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
constraint.priority=UILayoutPriorityDefaultHigh;
[self.view addConstraint:constraint];
//*************************************************************
//Setup some text
//label0.text=@"Label0";
label0.text=@"Some long textasdfasdfadsfasfdasf";
textField0.text = @"textField0";
//label1.text=@"Some long text";
label1.text=@"Label1";
textField1.text = @"textField1";
[self.view setNeedsLayout];
}