Search code examples
swiftuiscrollviewuitextview

Swift: UIScrollView doesn't scroll after the user edits the height of a UITextView


I have two UITextView inside a UIScrollView and the TextView can resize as the user types more text into them. I need the page to be scrollable to account for this so I put them in a ScrollView, however the height of the ContentView doesn't change with the TextViews. Here's what I tried so far:

  • adding a bottom constraint from the last helper text to the bottom of the content view with priority 1000 and constant 210. This doesn't work because the section will be scrollable before the TextViews get taller and on tablets or larger devices it will make my first TextView taller.
  • adding a bottom constraint with a constant of 210 or lower will give me a layout error.
  • I also thought about calculating the height of the content view and maybe adjusting the constraint based on that but this doesn't work either.

Here is a link to the file in question: https://www.dropbox.com/s/g7q1lbj0pxx14jh/Main.storyboard?dl=0

Any suggestions?

It's also worth noting that the keyboard will cover the second TextView when the first one gets too big so I will eventually need to edit that constraint based on the height of the first TextView (to give it more padding at the bottom so that the user can bring the second TextView in view.


Solution

  • Here is the fixed variant https://www.dropbox.com/s/gujry7ngziaipm7/Main.storyboard?dl=0

    I found just 1 error:

    You forgot to specify bottom offset from last view in View Container which is UILabel - "Being clear...", so your view container doesn't know when to resize as soon as you do not specified all offsets to it.