本文介绍了滚动型的iOS需求y位置或高度约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-ViewController
--View
---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0)
----ContentView (Top,Bottom,Leading,Trailing spaces to superview set to 0, Width equals with View (ViewController's child))
-----Label1
-----etc...

如果我设置一个特定的高度约束我的内容视图(如1000)的滚动型工作正常,但我得到了1000的静态内容查看,这不是我的目标,因为我的内容查看了动态内容,所以它应该是高度的内容需求。

If i set a specific height constraint for my content view (e.g. 1000) the ScrollView works fine, but i got a static ContentView of 1000, which is not my goal, because my contentView got dynamic content, so it should be the height the content needs.

如果我删除我的内容查看的高度约束,X code说:

If i delete the height constraint for my ContentView, Xcode says:

Missing Constraints:
ScrollView need constraints for: Y position or height

滚动型是在顶部和底部实际上牵制,所以我不知道为什么X $ C $Ç要对滚动型的高度约束......

ScrollView is actually pinned at top and bottom, so i don't know why Xcode want's a height constraint for the ScrollView...

如何走正确的路,当我想要的内容查看高度是高度的内容需求?

How is the right way to go, when i want the ContentView height to be the height the content needs?

推荐答案

每当使用滚动,支持自动布局始终遵循下面的步骤,

Whenever using ScrollView with auto layout always follow below steps,


  1. 滚动型约束:leadingSpace,topSpace,TrailingSpace,bottomSpace到上海华,并确保当你控制拖动来添加约束由pressing ALT添加它,这样的约束将无保证金设定

  1. ScrollView constraints: leadingSpace, topSpace, TrailingSpace, bottomSpace to superView and make sure when you control drag to add constraint add it by pressing alt so that the constraint would be set without margin.

滚动视图中添加的UIView的内容视图并设置其约束条件:
leadingSpace,topSpace,trailingSpace,bottomSpace以滚动型无pressing ALT键,并设置为equalWidth滚动型。

Add UIView inside scroll view as content view and set its constraints:leadingSpace, topSpace, trailingSpace, bottomSpace to ScrollView without pressing alt button and set equalWidth to ScrollView.

在此内容视图中添加任何意见必须有自上而下的约束这是所有视图的应该有垂直约束,所以内容查看可以根据里面的内容计算出自己需要的高度。

Whatever views you add inside this content view must have top to bottom constraint that is all view's should have vertical constraint, so contentView can calculate height required for itself based on the content inside it.

滚动型约束:

ContainerView约束:

ContainerView constraints:

这篇关于滚动型的iOS需求y位置或高度约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 13:06