3

我有四个标签堆叠在前一个下方,但将其基线与其内容视图的顶部对齐,而不是彼此垂直间距。

我是这样用代码做的

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topFirstLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:20.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topSecondLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:47.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topThirdLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:70.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topFourthLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:87.0f]];

现在,我希望所有标签都通过尾随空间与其父视图对齐。

我可以使用唯一的 VFL 字符串来做到这一点吗?像这样的东西,虽然这个例子会使应用程序崩溃:

NSDictionary *views = NSDictionaryOfVariableBindings(contentView, topFirstLabel_, topSecondLabel_, topThirdLabel_, topFourthLabel_);
NSDictionary *metrics = @{ @"bigMargin" : @12 };

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[topFirstLabel_][topSecondLabel_][topThirdLabel_][topFourthLabel_]-bigMargin-|"
                                                                    options:NSLayoutFormatAlignAllTrailing
                                                                    metrics:metrics
                                                                      views:views]];
4

2 回答 2

2

这个可能会帮助你。它使用垂直线性布局概念,您可以根据需要添加填充。

于 2013-08-12T19:46:37.607 回答
1

我不认为你可以在一个电话中做到这一点。

您可能可以执行以下操作:

for ( NSString* viewName in views.allKeys )
{
    [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat: @"H:[%@]-bigMargin-|", viewName], options:NSLayoutFormatAlignAllTrailing metrics:metrics views:views];
}
于 2013-08-07T23:19:58.877 回答