填充UIScrollView的垂直空间 [英] Fill Vertical Space of UIScrollView

查看:50
本文介绍了填充UIScrollView的垂直空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有 UIScrollView 的布局,我想要做的是将标签固定在可滚动区域的顶部,将按钮固定在底部,两者之间的距离要最小.

I'm working on a layout that has a UIScrollView, and what I want to do is have a label pinned to the top of the scrollable area, and a button pinned to the bottom, with a minimum amount of space between the two.

这个想法是,如果标签的高度增加,则按钮将被向下推动,因此用户必须向下滚动才能到达该标签.但是,如果标签较短,则标签和按钮之间的距离应增加,以使按钮停留在可滚动区域的底部.

The idea is that if the label grows in height the button will be pushed downwards so that the user has to scroll down to get to it. However, if the label is short, the space between it and the button should grow so that the button stays at the bottom of the scrollable area.

下面是我的意思的粗略模型:

Below is a rough mockup of what I mean:

注意:第二张镜头是一直滚动到 UIScrollView 的底部之后,我希望在滚动到顶部时隐藏该按钮;即,仅将按钮置于滚动视图下方是不可行的.

NOTE: The second shot is after scrolling all the way to the bottom of the UIScrollView, I want the button to be hidden when scrolled to the top; i.e- simply placing the button below the scroll view is not an option.

推荐答案

仅使用自动布局即可获得所需的结果,而无需通过代码更改约束.步骤如下:

You can achieve the desired result with autolayout only, without having to change constraints via code. The steps are as follows:

1)创建滚动视图并将其固定到所有侧面:

1) Create your scrollview and pin it to all sides:

2)在滚动视图中添加一个将用作内容视图的视图,并将其固定:

2) Add a view, which will act as content view, to the scrollview and pin it as well:

3)您的标签和按钮将被添加到此contentView,但是在执行此操作之前,我们必须向contentView添加2个附加约束.为contentView的宽度和高度创建与SafeArea相等的约束.您应将等高约束的优先级设置为低(250):

3) Your label and button will be added to this contentView, but before we can do this, we must add 2 additional constraints to the contentView. Create constraints for the width and height of the contentView to be equal as the SafeArea. You should set the priority of the equal heights constraint to low (250):

4)在contentView中添加标签和按钮.标签固定在顶部,按钮固定在底部;

4) Add your label and button inside the contentView. The label pinned to the top and the button pinned to the bottom;

5)最后,在标签和按钮之间添加垂直间距约束.将按钮和标签之间的最小最小间距设置为此约束的常数,例如8,并将该关系设置为大于"或等于":

5) Lastly, add a vertical spacing constraint between the label and the button. Set the desired minimum amount of space between the button and the label as this constraint's constant, for instance 8, and set the relation to be Greater Than or Equal:

这样,如果标签的高度增加,则按钮将被向下按下,因此用户必须向下滚动才能到达标签.

With this, if the label grows in height, the button will be pushed downwards so that the user has to scroll down to get to it.

这篇关于填充UIScrollView的垂直空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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