2013-07-10 27 views
3

我有四个标签,其中一个在前一个下方堆叠,但其内容视图的顶部与其基线对齐,而不是相互之间的垂直间距。自动布局VFL:将水平约束中的所有尾部对齐

我这样

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

我不认为你能做到这一点 - 你必须让格式字符串中的所有标签对齐它们,但是不能设置其他的间距,因为你已经完成了其他方法。为什么不让标签在垂直方向上彼此间隔(而不是内容视图),那么您可以使用VFL来执行该间距以及对齐所有后边缘? – rdelmar

+0

@rdelmar我不能这样做,我必须将每个单元格从其超级顶部边缘到基线的固定空间对齐。这里没有什么可做的:P感谢您的回应。 – emenegro

回答

2

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

1

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

你或许可以做这样的事情:

for (NSString* viewName in views.allKeys) 
{ 
    [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat: @"H:[%@]-bigMargin-|", viewName], options:NSLayoutFormatAlignAllTrailing metrics:metrics views:views]; 
}