使用自动布局时计算UIScrollView的contentSize [英] Calculating contentSize for UIScrollView when using Auto Layout

查看:154
本文介绍了使用自动布局时计算UIScrollView的contentSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于某些有用的东西的快速问题,但可以写得更好。我有一个 UIScrollView 以及一个在另一个下面放置的对象列表。一切都在 viewDidLoad()期间完成,对象的位置使用自动布局。以下是我将 UIScrollView contentSize 高度设置为适当的值。

Here is a quick question about something which works, but could be written much better. I have a UIScrollView and 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 contentSize height of the UIScrollView to its appropriate value.

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

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

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.

推荐答案

以编程方式提供内容大小并不是一个好方法。以下解决方案将
使用autolayout工作,根本不需要设置内容大小。它将根据
计算添加到查看的UI字段数。

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.

第1步:

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

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

第2步:

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

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.

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

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

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

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).

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

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步:根据您的要求为您在内容视图中添加的所有视图添加约束。

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

供参考:

我希望这肯定对你有帮助。

I hope this will definitely help you.

这篇关于使用自动布局时计算UIScrollView的contentSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆