UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to correctly size UITextView based on content in Table View header?

Forums > iOS

Hello all,

I am finishing Dynamic Type support in my app but UITextView is giving me a hard time.

I have Text View as part of table view header but I cannot get it to size according to content. It has no height constraint, scrolling is disabled and I am calling sizeToFit() after setting its content. This somewhat works but if I have text that is three lines where the last line is just a single word it is not visible.

The text view grows kind of correctly when I type and press return but without it it does not. I also tried calling sizeToFit() in the delegate method func textViewDidChange(_ textView: UITextView) but that did not help.

And another problem (:D) the Text View and table view header grows with it but it starts to cover the rest of the table view content instead of pushing it down. I tried calling layoutIfNeeded() on table view as well as the old trusty reloadData() but without any effect.

I will be super happy if anyone can provide hints what to try next.

3      

Did you lay out your constraints for your header view (I presume that it's a dedicated view) in code? If so, have you tried setting its translatesAutoresizingMaskIntoConstraints property to false?

3      

I have the constraints in storyboard and they work fine until I want to resize the text view when user enters new content.

3      

Any tips? :-)

I could possibly move this stuff to separate table view cells but since I am ever going to have one UITextView that seems like needlesly complicated solution.

3      

I tried a lot of things but it simply would not work. The solution was to create custom table view cell with the content and resizing works great.

I guess the takeaway is to go for cell for these complex cases outright instead of trying to use the table view header. I am going to use it for only static content from now on.

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.