wyu
wyu

Reputation: 1813

How to have a UIScrollView fit its content size, up until a max before scrolling in autolayout

I want to create a UIScrollView in autolayout that will fully contain it's content (does not scroll) until the content is a certain size (say 400), and then it starts scrolling.

So I have my views laid out like thisUIScrollView

If I set Scroll View.height = ContentView.height, then the scrollview expands to fit all of the content. So far so good.

Now if I try to set a max height via Scroll View.height <= 400, what ends up happening is it sets the ContentView.height as well (due to Scroll View.height = ContentView.height) and the text becomes cut off and it doesn't scroll

cut off text

So is this even possible to do with autolayout? This seems easy to do with frames but I'd like a solution in autolayout.

Here's the sample project if you'd like to play around yourself - https://github.com/iwllyu/content-hugging-uisv-with-max

Upvotes: 7

Views: 2593

Answers (1)

srvv
srvv

Reputation: 623

You have got everything else right. But for this to work you should set priority of Scroll View.height = ContentView.height constraint less than the compression resistance priority of your subview (i.e. UILabel here).

So in your case anything below 750 will work.

Upvotes: 7

Related Questions