带有内容视图的 UIScrollView [英] UIScrollView with Content View

查看:20
本文介绍了带有内容视图的 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为用户注册表创建 UIScrollView,但没有成功.我正在使用自动布局,滚动视图中的所有字段都是静态的.由于通常不明确的高度问题,我在滚动视图中添加了一个 UIView,将约束设置为滚动视图的边距并将其居中对齐.之后,我在情节提要中添加了该内容视图中的所有字段.

内容字段的约束设置如您所料,但是当我到达最低字段并将底部约束设置为内容视图的底部时,一切都会中断.

Xcode 要求我设置某些视图的优先级,当我按原样操作时,内容视图大小保持不变,视图缩小.

我尝试不放置最后一个底部约束并通过代码调整内容视图的大小,但高度未按原样调整大小.

我正在寻找一个很好的解决方案来在故事板和自动布局中做到这一点.

更新:我添加了一个低优先级的底部约束,但内容滚动视图并未展开以显示所有字段.

解决方案

您正在使用自动布局,因此内容视图的大小由约束决定.请按照以下步骤提供适当的约束:

<块引用>

  1. 在主视图内拖动滚动视图并提供与超级视图(主视图)对齐的顶部、底部、前导和尾随约束每个屏幕截图.

<块引用>

  1. Take View 将包含您的内容并拖动到 Scroll View 中.并提供约束中心 X、中心 Y、顶部、底部、根据屏幕截图,前导和尾随与滚动视图对齐

<块引用>

  1. 将所有元素放在内容视图中,内容视图是滚动视图的子视图,并提供相对于上面元素的顶部约束它,使元素之间的空间相等(例如标签、按钮等)(确保为 X 位置提供所需的约束)

  2. 最后一个元素是注册帐户";按钮确保您提供相对于国家的顶部约束和相对于的底部约束超级视图(内容视图)并更改顶部或底部的优先级按截图约束,否则报错.

I've been trying to create a UIScrollView for user registry but with no success. I'm using auto layout and all of the fields that go inside the scroll view are static. Because of the usual ambiguous height issue, I've added a UIView inside the scroll view, set the constraints to the margins of the scroll view and centered aligned it. After that I added all of the fields inside that Content View, in the storyboard.

The content fields have their constraints setup as you would expect, but when I get to the lowest field and set the bottom constraint to the bottom of the Content View then everything breaks.

I'm asked by Xcode to set the priority of some views, and when I do as is says, the Content View size stays the same and the views are shrunken.

I tried not to put the last bottom constraint and resize the Content View by code but the height is not resized as is should.

I'm looking for a good solution to do this in storyboards and auto layout.

Update: I added a bottom constraint with a low priority, but the content scroll view is not expanding to show all of the fields.

解决方案

You are using auto layout so the size of the content view is determined by constraints. Follow the below steps to provide proper constraints:

  1. Drag the Scroll View inside main view and provide constraints Top, Bottom, Leading and Trailing in align with Super View (Main View) as per screenshot.

  1. Take View which will contain your content and drag inside Scroll View. and provide the constraints Center X, Center Y, Top, Bottom, Leading and Trailing in align with Scroll View as per screenshot

  1. Put all the element inside content view which is a subview of scroll view and provide Top constraint relative to the element above it, to make equal space between the elements (eg. label, button etc.) (Make sure you provide required constraint for X-position)

  2. Last element is "Register Account" button make sure you provide the Top Constraint relative to country and Bottom constraint relative to superview (content view) and change the priority for Top or Bottom constraint as per screenshot, otherwise it gives error.

这篇关于带有内容视图的 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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