简体   繁体   English

iOS将子视图添加到具有动态高度的滚动视图

[英]iOS add subviews to a scroll view with dynamic heights

The problem I am forcing is: I have 1 big scrollView and I have to add to it 5 subviews programmatically when I don't know what heights the sub views will have :( 我强迫的问题是:我有1个较大的scrollView,当我不知道子视图的高度时,必须以编程方式添加5个子视图:(

Is there some API to do so ? 是否有一些API可以这样做? or I have to count the subviews heights manually and add them with CGRectMake based by calculated sizes? 还是我必须手动计算子视图的高度, CGRectMake根据计算出的大小将它们与CGRectMake

What I would do is use Auto Layout. 我要做的是使用自动布局。 If you add your subviews to the scroll view using internal constraints, then these constraints can determine not only the distance of the subviews from one another but also the distance from the imaginary content view that surrounds them. 如果使用内部约束将子视图添加到滚动视图,则这些约束不仅可以确定子视图彼此之间的距离,还可以确定其周围的虚构内容视图的距离。 The scroll view then uses the size of the content view as its contentSize , and the whole thing becomes scrollable. 然后,滚动视图使用内容视图的大小作为它的contentSize ,整个对象变为可滚动的。 And this works despite the fact that you don't know the heights of any of the views, which is exactly what you're after. 尽管您不知道任何视图的高度,但这仍然可行,而这正是您所追求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM