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

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

问题描述

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

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.

推荐答案

以编程方式提供内容大小不是好方法.以下解决方案将使用自动布局工作,根本不需要设置内容大小.它将根据添加了多少 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 步:

添加滚动视图以在情节提要中查看并添加前导、尾随、顶部和底部约束(所有值都为零).

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

第 2 步:

不要直接在scrollview上直接添加你需要的view,先添加一个view滚动视图(这将是我们所有 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) 向主视图(即包含滚动视图)添加等高、等宽.对于相等的高度,将优先级设置为低.(这是设置内容大小的重要步骤).

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.

供参考:

我希望这对你有帮助.

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

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