我知道这个问题已经被问过很多次了,但这是我长期以来一直在苦苦挣扎的问题,而且即使现在有最新的答案和教程,我也相信其他人也会遇到这个问题。
添加滚动视图时,请执行以下步骤:
在视图控制器中将滚动视图添加为原始视图的子视图。固定顶部,左侧,右侧和底部。确保未选中“限制到边距”。
添加一个UIView作为滚动视图的子视图。固定顶部,左侧,右侧和底部约束。
在内容视图和视图控制器视图之间添加相等的宽度约束。
在我运行应用程序的这一点上,内容视图没有出现,滚动视图占据了整个屏幕。
此时,当我运行该应用程序时,“内容视图”和“滚动视图”都不会占据屏幕的大约一半,并且可以滚动“内容视图”。见下面的照片。
我遵循了我能找到的所有教程,并尝试实现我发现的所有SO答案,但是我似乎无法使其正常工作。如果有人遇到或知道解决方案,将非常感谢您的帮助!
绿色是内容视图,蓝色是滚动视图
滚动视图和子视图约束
I figured this out with the help of the other answers but I had to make some adjustments to get it work the way I wanted. Here are the steps I took:
Add a Scroll View as a Sub View of the Main View.
Select the Scroll View and uncheck "Constrain to margins" and pin top, left, right, bottom, constraints
Add a UIView as a subview of the Scroll View. Name this view "Content View"
Select the Content View and pin top, left, right, and bottom constraints. Then add a center horizontally constraint.
Next from the Content View to the Main View add equal width and equal height constraints.
Add whatever elements you need inside the Content View. Pin top, left, right, and height constraints to the elements that were just added.
在“内容视图”内最底部的项目固定一个底部约束。选择此约束,然后更改为“大于或等于”。将该常数更改为20。
添加到“内容视图”内部项目的约束非常重要,尤其是添加到最后一个项目的底部约束。它们有助于确定滚动视图的内容大小。如我所述,添加底部约束将使视图在内容太大而无法容纳在屏幕中时滚动,而在内容确实适合在屏幕中时禁用滚动。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句