固定UIScrollView的子视图,而其他子视图可滚动 [英] Make subview of a UIScrollView fixed while the other subviews scrollable
问题描述
我想创建一个视图,其中三个子视图堆叠在一起,中间子视图可滚动,其他子视图固定。
I want to make a View with three subviews stacked on top of each other with the middle subview scrollable with the others fixed.
如何以编程方式实现此目的?我已经尝试了
How can I achieve this programmatically? I have tried
- 将根视图的内容大小设置为可滚动视图的大小,但这会使所有视图都滚动。
- 设置中间子视图的内容大小而不设置根视图的任何属性,但这使得所有视图都不可滚动。
-set the contentsize of the middle subview without setting any property for the root view but that makes all the views unscrollable.
请帮忙。我是iOS新手。
Please help. I am new to iOS.
提前致谢
推荐答案
你可以在 UIScrollView
上使用 scrollViewDidScroll:
委托回调来调整视图的位置。在回调中,获取scrollview的 contentOffset
并使用它来设置固定视图的位置。
You can use the scrollViewDidScroll:
delegate callback on the UIScrollView
to adjust your view's position. In the callback, get the contentOffset
of the scrollview and use that to set your fixed view's position.
例如,如果你希望你的固定视图始终保持100像素的滚动视图顶部,将其初始帧设置为(0,100,宽度,高度),然后在回调中将帧设置为(0,contentOffset.y + 100,宽度,高度)。
For example, if you want your fixed view to always remain 100 px from the top of the scrollview, set its initial frame to (0, 100, width, height), and then in the callback set the frame to (0, contentOffset.y + 100, width, height).
结果是子视图看起来固定在给定高度。
The result is that the subview will appear fixed at a given height.
这篇关于固定UIScrollView的子视图,而其他子视图可滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!