NSLayoutConstraint设置

Pad*_*215 1 objective-c ios autolayout nslayoutconstraint

我正在尝试设置一个面板来显示一些信息.我无法按照自己的意愿将autolay运行起来.

这是我正在尝试做的事情:

在此输入图像描述

我有一个headerView,一个UIImageView和5个标签.我希望标签垂直排列,我希望它们与UIImageView有8个间距.我可以很容易地垂直排列它们:

[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)]];
Run Code Online (Sandbox Code Playgroud)

但让它们排列在图像视图之外是一种繁琐/冗长的方式:

[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)]];
Run Code Online (Sandbox Code Playgroud)

似乎是一种更好的方法,然后为图像视图的每个标签设置约束.我尝试添加NSLayoutFormatAlignAllCenterX到第一个约束,但它将它们全部设置为内容视图的中心X,不会将标签的中心X相互对齐.NSLayoutFormatAlignAllBaseline在垂直使用时给出错误和崩溃.

我将不得不再次为数据做这个(英亩:'价值').

Joh*_*uer 6

你正走在正确的轨道上NSLayoutFormatAlignAllCenterX.您需要做的就是V:[headerView]-[acresLabel]与约束的其余部分隔离,因为您不希望将headerViewCenterX与5个标签对齐.

// 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] ;
Run Code Online (Sandbox Code Playgroud)