【发布时间】:2013-07-08 07:03:01
【问题描述】:
我有四个标签堆叠在前一个下方,但其基线与其内容视图的顶部对齐,而不是彼此垂直间距。
我是这样写代码的
[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]];
【问题讨论】:
-
我不认为你能做到这一点——你必须让格式字符串中的所有标签对齐它们,但你不能设置其他间距,因为你'已经用其他方法做到了。为什么不让标签相对于彼此垂直间隔(而不是写到 contentView),然后你可以使用 VFL 来做间隔以及对齐所有后沿?
-
@rdelmar 我不能这样做,我必须将每个单元格从其超级视图顶部边缘对齐到基线的固定空间。无事可做:P 感谢您的回复。
标签: ios autolayout visual-format-language