xcode 在自动布局中缺少 UIScrollView 的约束 x 和高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33860294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:14:47  来源:igfitidea点击:

Missing constraint x and height to UIScrollView in autolayout

iosxcodeuiviewuiscrollviewautolayout

提问by Kalzem

Disclaimer: I saw a SO technique which includes adding an UIView (being called a contentView) to the UIScrollView and putting everything to this contentView. I don't want to use this technique as I would like to understandwhy I am having the current problem.

免责声明:我看到了一种 SO 技术,其中包括将 UIView(称为 contentView)添加到 UIScrollView 并将所有内容放入此 contentView。我不想使用这种技术,因为我想了解为什么我会遇到当前的问题。

I have a UIScrollView on the default UIView of a UIViewController, its UIScrollView has 4 constraints: One on each side (trailing, top, leading, bottom) so that it glues to its parent the UIView. This works!

我在 UIViewController 的默认 UIView 上有一个 UIScrollView,它的 UIScrollView 有 4 个约束:每一侧(尾随、顶部、前导、底部)一个,以便它粘附到其父级 UIView。 这有效!

Working UIScrollView constraints

工作 UIScrollView 约束

But when I want to add an UIImageView to the UIScrollView and I want it to be glued to its parent sides (trailing, top, leading) + a specific height, now I have a problem.

但是当我想将 UIImageView 添加到 UIScrollView 并且我希望它被粘到它的父边(尾随、顶部、前导)+特定高度时,现在我遇到了问题。

Non working UIImageView constraints

非工作 UIImageView 约束

It says:

它说:

Scroll View: Has ambiguous scrollable height
Scroll View: Needs constraints for: X position or width

回答by seto nugroho

The reason xcode throws those error is because it cannot calculate UIScrollViewand UIImageViewframe in the runtime.

xcode 抛出这些错误的原因是它无法在运行时计算UIScrollViewUIImageView框架。

when you add constraint for UIScrollViewrelated to its superview, it tells xcode to draw scroll view as big as its superview. However, when you add constraint for UIImageViewrelated to UIScrollViewsize, it tells xcode to draw image view as big as UIScrollViewcontent size. please note that UIScrollViewsize and content size is two different thing.

当您添加UIScrollView与其父视图相关的约束时,它会告诉 xcode 绘制与其父视图一样大的滚动视图。但是,当您为UIImageView相关UIScrollView大小添加约束时,它会告诉 xcode 绘制与UIScrollView内容大小一样大的图像视图。请注意,UIScrollView大小和内容大小是两个不同的东西。

if we think of it as a house, scroll view size is basically window size, it is a size that you can use to see the inside of the house. And content size is the size of the house. which is why there is no relation between scroll view's size and content size.

如果我们把它想象成一个房子,滚动视图的大小基本上就是窗口的大小,它是一个可以用来查看房子内部的大小。内容大小是房子的大小。这就是为什么滚动视图的大小和内容大小之间没有关系的原因。

you can fix your problem in two ways, first by set scroll view content size either programmatically or by using IB. Although, I'm not sure if setting it programmatically will silence the warning. You can set it in IB by following the answer here. second ways involving adding more constraint to your UIImageView. By adding bottom constraint and width constraint, xcode can calculate UIScrollViewcontent size which will remove this warning.

您可以通过两种方式解决您的问题,首先通过以编程方式或使用 IB 设置滚动视图内容大小。虽然,我不确定以编程方式设置它是否会使警告静音。您可以按照此处的答案在 IB 中进行设置。第二种方法涉及向您的UIImageView. 通过添加底部约束和宽度约束,xcode 可以计算UIScrollView将删除此警告的内容大小。

回答by Raxak

To solve this issue, I did the following thing: 1. Inserted a scrollview. Gave it constraints to Superview from all directions 2. Inside Scrollview, I added an another UIView.Then, I added similar constraints to Superview from all directions to the UIView as well. In addition to this, I gave it Vertically center alignment and Horizontally center Alignment. This removed the constraints errors.

为了解决这个问题,我做了以下事情: 1. 插入一个滚动视图。给它从所有方向的 Superview 约束 2. 在 Scrollview 中,我添加了另一个 UIView。然后,我也从所有方向向 UIView 添加了对 Superview 的类似约束。除此之外,我给它垂直居中对齐和水平居中对齐。这消除了约束错误。

回答by user2072000

This worked for me.

这对我有用。

Do the following in your storyborad

在你的故事板中执行以下操作

  • Add a hidden view of height placed at 0,0 in your scrollview.
  • Add constraints to the hidden view , top = 0 , left = 0, right = 0, bottom = 0.
  • Add the constraint on this hidden view to make it center aling horizontally to the scroll view.
  • 在滚动视图中添加一个高度为 0,0 的隐藏视图。
  • 向隐藏视图添加约束,top = 0,left = 0,right = 0,bottom = 0。
  • 在这个隐藏视图上添加约束,使其水平居中对齐滚动视图。

This should take care of the scrollview complaining about the x position.

这应该处理抱怨 x 位置的滚动视图。