ios 使用自动布局时计算 UIScrollView 的 contentSize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38948904/
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-08-31 09:54:50  来源:igfitidea点击:

Calculating contentSize for UIScrollView when using Auto Layout

iosswiftuiscrollviewautolayout

提问by Michel

Here is a quick question about something which works, but could be written much better. I have a UIScrollViewand a list of objects laid out inside, one under the other. Everything is done during viewDidLoad()and the placement of the objects uses Auto Layout. Here is what I do to set the contentSizeheight of the UIScrollViewto its appropriate value.

这是一个关于有效但可以写得更好的快速问题。我有一个UIScrollView和一个里面布置的对象列表,一个在另一个下面。一切都在使用期间完成,viewDidLoad()并且对象的放置使用Auto Layout. 这是我将 的contentSize高度设置UIScrollView为其适当值的方法。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    globalView.contentSize = CGSize(width: globalView.frame.width,
                                    height: globalView.frame.height * 1.59)
}

It works, but the arbitrary value 1.59 has obviously been decided by me trying a few possible values. What is the proper way to compute the contentSize in such a case?I am doing everything programmatically. Searching the net didn't lead me to any simple and clear answer, so eventhough it may be a somewhat duplicate question I decided to reformulate it.

它有效,但任意值 1.59 显然是由我尝试一些可能的值决定的。在这种情况下计算 contentSize 的正确方法是什么?我正在以编程方式做所有事情。在网上搜索并没有让我得到任何简单明了的答案,所以尽管这可能是一个有点重复的问题,我还是决定重新表述它。

回答by Shrikant K

Giving content size programatically is not good way. The below solution which will work using autolayout, dont need to set content size at all. It will calculate as per how many UI fields added to view.

以编程方式提供内容大小并不是一个好方法。以下解决方案将使用自动布局工作,根本不需要设置内容大小。它将根据添加到视图中的 UI 字段的数量进行计算。

Step 1 :

第1步 :

Add Scrollview to view in storyboard and add leading, trailing, top and bottom constraints (All values are zero).

添加滚动视图以在故事板中查看并添加前导、尾随、顶部和底部约束(所有值均为零)。

Step 2 :

第2步 :

Don't add directly views which you need on directly scrollview, First add one view to scrollview (that will be our content view for all UI elements). Add below constraints to this view.

不要在直接滚动视图上直接添加您需要的视图,首先向滚动视图添加一个视图(这将是我们所有 UI 元素的内容视图)。向该视图添加以下约束。

1) Leading, trailing, top and bottom constraints (All values are zero).

1) 前导、尾随、顶部和底部约束(所有值都为零)。

2) Add equal height, equal width to Main view (i.e. which contains scrollview). For equal height set priority to low. (This is the important step for setting content size).

2)向主视图(即包含滚动视图)添加等高、等宽。对于相同的高度,将优先级设置为低。(这是设置内容大小的重要步骤)

3) Height of this content view will be according to the number of views added to the view. let say if you added last view is one label and his Y position is 420 and height is 20 then your content view will be 440.

3) 此内容视图的高度将根据添加到视图的视图数量而定。假设您添加的最后一个视图是一个标签,他的 Y 位置为 420,高度为 20,那么您的内容视图将为 440。

Step 3: Add constraints to all of views which you added within content view as per your requirement.

第 3 步:根据您的要求向您在内容视图中添加的所有视图添加约束。

For reference :

以供参考 :

enter image description here

enter image description here

enter image description here

enter image description here

I hope this will definitely help you.

我希望这肯定会帮助你。

回答by Michel

Though I finally got to solve this issue and make things work. I noticed that all I could find on the net as related answers was directed to storyboard users. But nothing to do it programmatically that I could put my hands on.

虽然我终于解决了这个问题并使事情顺利进行。我注意到我在网上能找到的所有相关答案都是针对故事板用户的。但是没有什么可以以编程方式完成的,我可以动手。

So I decided to make an extremely simple demo app to show how this can be achieved. I hope it will be useful to someone at some point.

所以我决定制作一个非常简单的演示应用程序来展示如何实现这一点。我希望它在某个时候对某人有用。

Here is the address to get it on GitHub: https://github.com/zaxonus/AutoLayScroll

GitHub上获取地址如下:https: //github.com/zaxonus/AutoLayScroll

回答by temlocke

I also used constraints programmatically to dynamically modify UIScrollView's contentSize . I pretty much followed Shrikant's instructions but for step 2.1, I set centerX to the scrollViews.centerX rather than set the leading and trailing margins (No idea why the former works, while the latter doesn't). Then as a last step, after adding the final subview, I did the following:

我还以编程方式使用约束来动态修改 UIScrollView 的 contentSize 。我几乎遵循了 Shrikant 的说明,但是对于步骤 2.1,我将 centerX 设置为 scrollViews.centerX 而不是设置前导和尾随边距(不知道为什么前者有效,而后者无效)。然后作为最后一步,在添加最终子视图后,我执行了以下操作:

contentView.layoutIfNeeded() //set a frame based on constraints
scrollView.contentSize = CGSize(width: contentView.frame.width, height: contentView.frame.height)

Hope this helps somebody in the future.

希望这对将来的人有所帮助。

回答by barryjones

If you don't want to use storyboards, and just use constraints, you can follow the guide here

如果您不想使用故事板,而只想使用约束,则可以按照此处的指南进行操作

https://redflowerinc.com/implement-uiscrollview-using-constraints-no-need-to-use-content-size/

https://redflowerinc.com/implement-uiscrollview-using-constraints-no-need-to-use-content-size/

Use can use bottom anchor and pin your views to the scroll view. By using constraints you don't need to use content size.

使用可以使用底部锚点并将您的视图固定到滚动视图。通过使用约束,您不需要使用内容大小。