Timm
Timm

Reputation: 169

Autolayout ScrollView with dynamic content

im going crazy on autolayout and dynamic scrollview heigth.

I want to make my scrollview content height based on the content in multiple textviews. Also the spacing between the textviews should stay the same.

Therefor i tried to make a small sample app with two textviews and a button which calls [textview sizeToFit] for each textview. Another thread suggested to set the bottom verticl spacing of the lowest view to the scrollview (see screen1)

Following problems:

Im glad for any help or step-by-step tutorials. Another thought I had: Would it make sense to put those textviews in a tableview and make cells heigth depending on content, so I dont habe to struggle with scrollview stuff?

More details are on the screenshots: constraints before sizeToFit after sizeToFit

Upvotes: 0

Views: 400

Answers (1)

Timm
Timm

Reputation: 169

I got it figured out, using a UITableView with a custom cell, containing a label on left side and a textview on the right side.

To get the dynamic height in UITableView I used the answer provided in this thread: Dynamic Height iOS7.

Further more I added 2 constraints to the UITextView (top and bottom to the cell's content view) to stretch the UITextView to same height as the cell.

Though its not the answer that i wanted (since i tried to use autolayout-only), I hope others can use this approach.

Upvotes: 0

Related Questions