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]];
我不认为你能做到这一点 - 你必须让格式字符串中的所有标签对齐它们,但是不能设置其他的间距,因为你已经完成了其他方法。为什么不让标签在垂直方向上彼此间隔(而不是内容视图),那么您可以使用VFL来执行该间距以及对齐所有后边缘? – rdelmar
@rdelmar我不能这样做,我必须将每个单元格从其超级顶部边缘到基线的固定空间对齐。这里没有什么可做的:P感谢您的回应。 – emenegro