Reputation: 681
I have a textView which i use as an input field for chatMessages. I want it to expand if there is more text than could fit in one row.
func textViewDidChange(_ textView: UITextView) {
self.textViewHeight.constant = textView.contentSize.height
}
I set a height constraint programmatically if the user enters something inside the textView.
<- For one liners it looks fine.
<- After you press enter it gets hidden.
<- The second enter shows it up again.
What am i doing wrong here? I try to recreate the look of whatsapp / imessage, where this hiding does not happen.
Upvotes: 1
Views: 342
Reputation: 6963
This is because you are changing the size in the wrong delegate function. The delegate textViewDidChange
is called after you finished editing (resignFirstResponder gets called).
You should put your code to change the height in this function instead:
optional func textView(_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool
So this will change the height of the UITextView
as you type not after you finished typing.
Upvotes: 2