I am writing from scratch, increasing the UITextView in my swift application. I put textView on view as follows:

It is located directly above the keyboard.
textView has limitations associated with view : leading , bottom , top and trailing , still = 4 .
view has the following limitations:
trailing , leading , bottom , top and height
height is the output in my code. I check how many lines are in the textView , and based on this I change the height :
func textViewDidChange(textView: UITextView) { //Handle the text changes here switch(textView.numberOfLines()) { case 1: heightConstraint.constant = 38 break case 2: heightConstraint.constant = 50 break case 3: heightConstraint.constant = 70 break case 4: heightConstraint.constant = 90 break default: heightConstraint.constant = 90 break } }
The number of lines above is calculated using this extension:
extension UITextView{ func numberOfLines() -> Int{ if let fontUnwrapped = self.font{ return Int(self.contentSize.height / fontUnwrapped.lineHeight) } return 0 } }
The initial height of the textView is 38 . The initial font size in textView is 15 .
Now it works well when the user starts typing a new line, but the textView not set within the full border of the view. I mean the fact that it looks like this:

and it should look like this:

Why is extra extra space added and how can I get rid of it?
Currently, when a new line appears, this is white space, but when the user scrolls the textView to center the text and get rid of the space - he left forever, the user cannot scroll it again, so there is a white line. So for me it looks like a problem with refreshing content, but maybe you know better - can you give me some tips?