【问题标题】:Swift+xcode - auto layout high as much as neededSwift+xcode - 按需自动布局
【发布时间】:2017-01-24 09:39:19
【问题描述】:

我是 swift 新手,所以我不熟悉自动布局。我有一个图像。图像右侧是标签。到正确标签的距离应该是 8。另外我在图像下方有一个标签。这里的距离也应该是 8。

我希望图片现在会自动调整大小。但是在图片上你可以看到它告诉我约束有问题。可能是因为我没有告诉图像总是使视图中的图片尽可能宽,而不是看它需要多高。但我也不知道如何添加这样的约束。你能帮帮我吗?

图像左上角的约束:

scissor.top = topMargin (so 0 to top)
scissor.leading = leadingMargin (so 0 to left)

剪刀和底部标签之间的约束:

label.top =scissor.bottom + 8 (so distance of 8 between them)

底部标签的约束:

label.bottom = bottomMargin (distance of 0 to bottom)
label.leading = leading.Margin (so dostance of 0 to the left)

右侧标签与剪刀之间的约束:

articleLable.leading = scissor.trailing + 8 (so distance of 8)

右侧标签的约束:

articleLable.top = topMargin
articleLable.trailing = trailingMargin

【问题讨论】:

  • UIImageView 的高度/宽度是多少?根据屏幕大小,它应该是静态的还是动态的?
  • @AhmadF 动态根据屏幕大小和高度自动适应
  • 您可以添加您对图像(可能还有标签)的所有约束吗?
  • @BenjaminLowry 添加了约束
  • @Sonius 那么约束错误是什么?它说你需要什么?

标签: swift xcode constraints


【解决方案1】:

您需要约束 imageView,因为 x-Code 不知道“尽可能宽”是什么意思。 我假设您的意思是 imageView 的左侧和 Cell 的左侧是相同的,并且 imageView 的顶部和 Cell 是相同的。 添加这两个约束应该可以解决问题。

【讨论】:

  • 我已经有两个约束: scissor.top = topMargin (所以 0 到顶部) scissor.leading =leadingMargin (所以 0 到左边)
【解决方案2】:

您可以点击图片中的红色图标了解详情 Image

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多