开发者

Make subview of a UIScrollView fixed while the other subviews scrollable

开发者 https://www.devze.com 2023-02-07 15:13 出处:网络
I want to make a View with three subviews stacked on top of each other with the middle subview scrollable with the others fixed.

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 c开发者_开发技巧an I achieve this programmatically? I have tried

  • to set the contentsize of the root view to the size of the scrollable view but that makes all the views scroll.

-set the contentsize of the middle subview without setting any property for the root view but that makes all the views unscrollable.

Please help. I am new to iOS.

Thanks in advance


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.

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.


If your UIScrollView has a superview (i.e. a container view), you can add your 'fixed' view as a subview of the superview instead of the UIScrollView. You'll only have to calculate your frame coordinates once.


You can do it moving sub view from UIScrollView to super view of scrollview like:

Place/set your button over scroll view (not inside scroll view) as shown here in this snapshot. And also set button constraints (position) with respect to super view of your scrollview.

Make subview of a UIScrollView fixed while the other subviews scrollable

Here is ref. snapshot of hierarchy of position of each view over each-other.

Make subview of a UIScrollView fixed while the other subviews scrollable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号