John Doe
John Doe

Reputation: 2491

How to set minimum height for UITextView that grows dynamically?

I have a GrowingTextView which increases its height when content size increases.

override viewDidLoad() {
    tv.translatesAutoresizingMaskIntoConstraints = false
    tvHeight.constant = 200
    tv.sizeToFit()
}

func textViewDidChange(_ textView: UITextView) {
    if textView.contentSize.height > 200 {
        textView.sizeToFit()
    }  
    updateScroll()
}

I want to set minimum height to the text view as 200, but it is not working. I tried in IB as well as code. The UI has the below components each with 16 as leading and trailing and 8 as top and bottom constraints.

UITextField - Title
GrowingTextView - tv
UITextField - Author
UITextField

screenshot

How to set the min height for text view?

Upvotes: 0

Views: 2217

Answers (1)

axel
axel

Reputation: 422

Using the library that you mentioned GrowingTextView in your comment you can use the parameter minHeight = 200.

tv.minHeight = 200

In that case I think you don't need the height constant tvHeight.constant = 200. Here is an example:

override viewDidLoad() {
   super.viewDidLoad()
   tv.translatesAutoresizingMaskIntoConstraints = false
   tv.minHeight = 200
   tv.sizeToFit()
}

Upvotes: 2

Related Questions