gpt4 book ai didi

ios - UILabel 和 NSLayoutConstraint 的垂直列表

转载 作者:行者123 更新时间:2023-12-01 19:01:23 24 4
gpt4 key购买 nike

我有一个 UILabel 的垂直列表年代:

enter image description here

我希望能够让所有标签与右侧的“:”对齐,并将间距保持在 superView 的左侧(createDate 标签保持不变,名称和年份标签会向右移动) .

代码:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(nameLabel)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[nameLabel]-[createDateLabel]-[yearLabel]" options:NSLayoutFormatAlignAllLeading metrics:nil views:NSDictionaryOfVariableBindings(headerView, nameLabel, createDateLabel, yearLabel)]];

编辑:

好的,在实现一些建议后:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(52)-[nameLabel]-[createDateLabel]-[yearLabel]" options:NSLayoutFormatAlignAllTrailing metrics:nil views:NSDictionaryOfVariableBindings(headerView, nameLabel, createDateLabel, yearLabel)]];

让他们都对齐:

enter image description here

我更愿意将它固定在 headerView 上,因此如果该 View 改变高度,我不需要重新编码 pin 空间。此外,如果我固定到 headerView,它会导致标签一直向右移动:

enter image description here

所以这可能只是一场失败的战斗。

我仍然需要弄清楚如何将它们固定在左侧并保持“:”对齐。现在,我固定 createDateLabel,因为当我从视觉上看它时,我可以看到它最宽。有没有办法让我知道哪个标签最宽?

最佳答案

您可以通过以下方式执行此操作:

  • 对齐不领先的后沿
  • 将 super View 中的前导空间固定为 >= [某个 const 值]。这将使标签与左边缘至少具有给定的间距。
  • 固定垂直间距

  • 如果您知道哪个标签最长,您也可以将引导空间的元素固定到您的常量。

    所有这些约束也可以在界面生成器中进行,这样您的生活会稍微轻松一些

    关于ios - UILabel 和 NSLayoutConstraint 的垂直列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22699350/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com