3

前のラベルの 1 つ下に 4 つのラベルを積み重ねていますが、そのベースラインをコンテンツ ビューの上部に揃えており、互いに垂直方向の間隔を空けていません。

私はこのようにコードでそれを行います

[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

1

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 に答える