Dynamically change parent UIView’s height when TextView height changes

I have been working on a new app for the last few weeks and yesterday I ran into a small problem. I have a UITextView in a UIView container and I want the UITextView’s height to dynamically change as the user type in it. However, I also want the parent container’s height to increase as well. I broke this problem down in two sections:

  1. Dynamically change UITextView’s height
  2. Dynamically change container’s height.

First let’s start with UITextView’s height. There are plenty of ways you can do this, I decided to use UITextViewDelegate and it’s function ‘TextViewDidChange()’ to calculate the height of the textview. This function is called every time user types in the textview. Since I am not using storyboards for this project I have set all the constraints programmatically. Obviously for a certain element to increase it’s height dynamically you need to keep any one side of the element constraint-free.

More