I want to make a UITextView in my section header adjust its height to its content. I tried setting its numberOfLines to 0, but that didn't work. Is there any way to do this?
Create a subclass of UITextView and add the following code in the layoutSubviews() method:
UITextView
layoutSubviews()
override func layoutSubviews() { super.layoutSubviews() self.sizeToFit() }
This will make the text view adjust its height to its content.
Here are a few ways to make a UITextView in a section header adjust its height to its content:
Use Auto Layout constraints to tie the UITextView's top, bottom, and leading (or trailing) edges to the edges of the view container.
Wrap the UITextView in a UIView and use Auto Layout constraints to tie the UIView's top, bottom, and leading (or trailing) edges to the edges of the view container.
Implement the UITextViewDelegate protocol's textViewDidChange: method and use that method to adjust the UITextView's height to its content.
Use a third-party library that provides auto-sizing capabilities for UITextView, such as IQTextView.