Search code examples
iosswiftuiscrollview

Increasing scrollable content height (from top)


What I want to do is increase the size of the scrollable content of my UIScrollView programatically (every time a button is pressed, UILabel is made and stacked one on top of another on scrollView), and add the extra height to the top of my current ScrollView, so that I can scroll further up (rather than further down).

I have actually figured out how to add more scrollable content space to my scroll view:

var contentRect = CGRect.zero
for view in scrollView.subviews {
    contentRect = contentRect.union(view.frame)
}
scrollView.contentSize = contentRect.size

However, the extra space being added to the scroll view is at the bottom of the scroll view, when I want it to be at the top (so that I can scroll further up). Is there any way I can do this? Hopefully this makes sense.


Solution

  • You can't add the extra height to the top. After increasing the size of contentSize, you need to update the frame of each view in the scroll view to shift them down.

    Another option would be to use a UIStackView or UITableView which support adding additional view/rows.