Dmitrue
Dmitrue

Reputation: 135

iOS 8 - UIScrollView doesn't scroll

I want to make vertical scrolling depends on content inside Content View. I've created following hierarchy: View -> Scroll View -> Content View.

After all these actions, Scroll View doesn't scroll at all (it just shrinks the content). It scrolls only if i add scrollView.contentSize.height to the viewWillLayoutSubviews(). However i don't want to hardcode this height value. I thought Autolayout should automatically calculate content height and add appropriate scrolling. Does anyone know the proper way to solve this problem?

Upvotes: 2

Views: 7621

Answers (4)

Yariv Nissim
Yariv Nissim

Reputation: 13343

• Then i added Same Width and Same Height constraints between Content View and Main View in order to Scroll View could calculate it's content size

Remove this part. Putting subviews within the scrollView and adding top, bottom, leading and trailing constraints to it should be enough.

When you created the equal heights constraint (and equal widths), you prevented your content from resizing to its intrinsic size, which is used by auto layout to calculate the scrollView.contentSize

You can keep the equal widths constraints if you want your scrollView to only scroll vertically.

Upvotes: 1

brandav
brandav

Reputation: 705

I had the same issue. I fixed it by deleting the scroll view's constraints and then setting the scroll view's frame to equal the view's frame in viewDidAppear:

self.scrollView.frame = self.view.frame;

The issue occurs when you set the scroll view's width and height in storyboard to equal the view so you can see the full content. It's perfect for editing, but when you run your app, the view's frame size is different based on the iPhone or iPad, but the scroll view's frame size doesn't change. It's still the same size that you set in the storyboard.

That's why the scroll view will only work when you hard code the content size to a randomly large value. That value has to be greater than the current scroll view's frame size in order for it to work. Once you set the scroll view's frame to equal the view's frame, the content size setting will work properly:

self.scrollView.contentSize = self.contentView.frame.size;

Upvotes: 2

NSGangster
NSGangster

Reputation: 2447

Why do you need a content view inside of your scrollView? It sounds like your Content View is whats re-sizing your content and your scroll view's content size is being set to your Content View which isn't being dynamically changed. Try laying out your content in the scroll view itself so your scroll views content size will actually change depending on what's inside. Make sure your updating scrollView.contentSize!

Upvotes: 0

ImWH
ImWH

Reputation: 944

The frame of content view should be larger than scrollview then set the contentSize to ensure the range of scrolling.

Upvotes: 0

Related Questions