Pergunta

I have four labels stacked one below the previous one but alighning its baseline with the top of its content view, not with a vertical spacing with each other.

I do it by code this way

[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]];

Now, I want all labels to be aligned by the trailing space with its superview.

Can I do that with an unique VFL string? Something like this, although this example will crash the app:

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]];
Foi útil?

Solução

This library might help you. It uses a vertical linear layout concept and you can add paddings as needed.

Outras dicas

I don't think you can do this in a single call.

You can probably do something like this:

for ( NSString* viewName in views.allKeys )
{
    [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat: @"H:[%@]-bigMargin-|", viewName], options:NSLayoutFormatAlignAllTrailing metrics:metrics views:views];
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top