2

I have a Scroll View, and inside it a UIView called "Content View", that contains all of my elements.

enter image description here

The Content view is equal to the Scroll view's top, bottom, leading and trailing. Also equal its width & height.

I also added self.scrollView.contentSize = self.contentView.frame.size in ViewDidLoad.

When I scroll I can see a scroll bar going up and down on the side, and it seems like the page length is correct according to the bar, but the content itself does not scroll as you can see here:

enter image description here

What am I doing wrong?

Thanks!

Rotem Yakov
  • 181
  • 1
  • 12
  • 1
    It Seems the content is not inside the scrollView. The scrollView is on top of contentView, Check the view hierarchy – Janmenjaya Nov 19 '18 at 11:57
  • can you remove safe area from the scroll view once and try? – Aakash Nov 19 '18 at 11:58
  • I agree with @Janmenjaya please post your full view hierarchy – Reinier Melian Nov 19 '18 at 12:04
  • @Janmenjaya I posted the full hierarchy, you can see the content view is indented under the scroll view. – Rotem Yakov Nov 19 '18 at 12:41
  • @Aakash how can I delete a safe area? – Rotem Yakov Nov 19 '18 at 12:42
  • Click on "Scroll View" -> Go to "Show the File inspector" -> Uncheck "Use Safe Area Layout Guides" – Aakash Nov 19 '18 at 12:46
  • I did it, still not scrolling + the scroll bar not showing anymore (Also, now on landscape mode the iPhone X notch covers some of the content) – Rotem Yakov Nov 19 '18 at 13:14
  • we can not spot the "Content View" in your screenshot. where is it? – hardik parmar Nov 19 '18 at 13:31
  • Sorry, it was "inner view". changed it. – Rotem Yakov Nov 19 '18 at 13:49
  • You say *"Content view is equal to the Scroll view's top, bottom, leading and trailing. **Also equal its width & height**."* and then you say *"`self.scrollView.contentSize = self.contentView.frame.size`"* ... so, you set contentView's height equal to scrollView's height, and then you set contentSize equal to contentView's size... so how can you expect scrolling? What else are you doing in code? It looks like you've added an empty scrollView on top of the storyboard scrollView – DonMag Nov 19 '18 at 14:22
  • You probably right. I saw this line here: https://stackoverflow.com/questions/2824435/uiscrollview-not-scrolling, and thought it would help. Without this line it behaves the same, not scrolling, except I also don't have the scroll bar on the side – Rotem Yakov Nov 19 '18 at 15:22
  • @RotemYakov - are you designing your scrollView in storyboard, ***and*** adding a scrollView as a subview in code at runtime? – DonMag Nov 19 '18 at 16:01

4 Answers4

8

I ran into this issue (or something very similar) where the scrollbar would scroll, but the content did not. It drove me crazy for a while because it seemed like everything inside of the scrollview was constrained correctly in Interface Builder. Finally, I noticed that the top item within the scrollview container (container = the view inside of the UIScrollView that contained all of my views) was constrained to the top by aligning with a view outside of the scrollview.

Once I removed the constraint that was aligning the top with an external view from the ScrollView and instead aligned it with the main container within the UIScrollView, everything worked fine. Hopefully that helps in case someone runs into this issue and the other mentioned solutions don't fix it.

John Holtkamp
  • 159
  • 1
  • 5
1

The Content view is equal to the Scroll view's top, bottom, leading and trailing. Also equal its width & height.

You need to remove the width and height constraints. Top, bottom, leading and trailing should stay. Also, remove that contentSize setting from viewDidLoad.

I assume you want vertical scroll. In that case, you should set contentView's width constraint to be equal to View's width (not scrollView's but View's which is the root of the hierarchy).

That should do the trick, assuming that vertical constraints of contentView's children are properly set up.

user3199693
  • 128
  • 4
  • 12
1

By default Content Layout Guides is enabled for a Scroll View. So when we pin the content view (main view inside the scroll view) on all 4 sides and equal width and equal height (with lower priority), it takes Frame Layout Guide for top constraint, this is the root cause of the issue where only scroll indicator moves and not the entire content.

You can uncheck Content Layout Guides from Scroll View Size Inspector. Or if you want to use it change the top constraint to Content Layout Guide instead of Frame Guide Layout and it worked as charm. Please refer below screenshot.

enter image description here

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jigaroza287
  • 635
  • 5
  • 13
0

The 1st thing I had to do is what user3199693 said: remove that line from viewDidLoad (that made the wrong scroll bar I saw to go away). Also I set contentView's width constraint to be equal to top View's width, as he said.

That was good, but still didn't solve the problem. What did help was equal the height of the content view with top view.

Got the idea from here.

Rotem Yakov
  • 181
  • 1
  • 12