Padin215
Padin215

Reputation: 7484

NSLayoutConstraint setup

I'm trying to setup a panel to display some information. I'm having trouble getting the autolay out to work as i want.

here's what I'm trying to do:

enter image description here

I have a headerView, a UIImageView and 5 labels. I want the labels to line up vertically and i want them 8 spacing from the UIImageView. I can line them up vertically easily enough:

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(headerView, acresLabel, addedLabel, typeLabel, zonesLabel, sceneLabel)]];

But getting them to line up off the image view is proving tedious/verbose:

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[acresLabel]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(imageView, acresLabel)]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[addedLabel]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(imageView, addedLabel)]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[typeLabel]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(imageView, typeLabel)]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[zonesLabel]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(imageView, zonesLabel)]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[sceneLabel]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:NSDictionaryOfVariableBindings(imageView, sceneLabel)]];

Seems like a better way to do this then set up a constraint for each label to the image view. I tried adding NSLayoutFormatAlignAllCenterX to the first constrain, but it sets them all center to the content view's center X, doesn't line the label's center X up with each other. NSLayoutFormatAlignAllBaseline gives an error and crash when used vertically.

I'll have to do this again for the data (Acres: 'value').

Upvotes: 1

Views: 3667

Answers (1)

John Sauer
John Sauer

Reputation: 4421

You're on the right track with NSLayoutFormatAlignAllCenterX. All you need to do differently is isolate V:[headerView]-[acresLabel] from the rest of the constraint, since you don't want headerView's CenterX to be aligned with the 5 labels'.

// NSDictionary* views ;
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[acresLabel]"  options:0  metrics:nil  views:views] ;
// Use any of the horizontal NSLayoutFormats (NSLayoutFormatAlignAllLeft, NSLayoutFormatAlignAllRight, NSLayoutFormatAlignAllLeading, NSLayoutFormatAlignAllTrailing, NSLayoutFormatAlignAllCenterX)
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|" options:NSLayoutFormatAlignAllCenterX  metrics:0  views:views] ;
[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[acresLabel]"  options:0  metrics:nil  views:views] ;

Upvotes: 6

Related Questions