【问题标题】:Auto layout VFL: align all trailing in an horizontal constraint自动布局 VFL:在水平约束中对齐所有尾随
【发布时间】: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


【解决方案1】:

我认为您不能在一次通话中做到这一点。

你可能可以这样做:

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

【讨论】:

    【解决方案2】:

    这个library 可能会对您有所帮助。它使用垂直线性布局概念,您可以根据需要添加填充。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-02
      • 2015-11-02
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多