Reputation: 33
I have a UIView as a subview of a UIScrollView. The height of UIView is 50 and the height of the UIScrollView is 500.
I want to scroll from 50 to 500 in the scroll view while having the position of the UIView stay locked to the top of the scroll view. How can I achieve this?
Upvotes: 0
Views: 353
Reputation: 179
Add a "Pin vertical space" constraint between the UIView and another view that is outside of the UIScrollView to lock the UIView in place when the UIScrollView is scrolling.
See here: https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html
In above link, look at the notes for UIKit -> Autolayout support for UIScrollView
Copy pasting from the iOS release notes: "Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview"
Upvotes: 2
Reputation: 583
You could implement the scrollViewDidScroll:
delegate method of UIScrollView
and change the frame of the subview based on the current content offset of the scroll view.
Upvotes: 0