Nested UIScrollviews behaving strangely

Viewed 196

I have the following autolayout-driven setup:

  • Main viewController, with a scrollview inside it. Scrollview pinned to superview edges. This one scrolls up and down. A few normal, fixed size views at the top of the scrollview

  • Another scrollview. This one scrolls left and right. The second scrollview contains a couple of tableviews, side by side. The idea is that the user can switch between them. They both contain a handful of cells, all the same width as the screen and 72pts tall.

The problem I'm trying to solve is that the tableview contents are not the same size. The left one has say, 6 cells, and the right one has 3.

My first approach was to dynamically change the second scrollview height to match which ever tableview was currently visible. What ended up happening was that switching between the two tableviews (by doing setContentOffset:animated:) went extremely wrong if animated was set to true - it would adjust the content offset so everything was offscreen. In fact it would set the content offset to and then as I switched, about a dozen times, then it'd reset. It was weird, I gave up.

Now I'm trying to just adjust the content inset of the main scrollview to offset the gap in the content of the current tableview, and it's also being weird. When I set the bottom content inset in viewDidLoad, it works fine. When I set it at the time the tableview becomes current, it does nothing.

What gives? What scenarios would lead to these view interactions not behaving properly?

1 Answers
  • Use different tableViewController for each table.
  • Embed them in pageViewController.
  • Add those few normal, fixed size views at the top of the pageViewControllers view.
  • Conform scrollViewDelegate in pageViewController.
  • Pass scrollViewDidScroll from tableViews to pageViewController.
  • Set tableViews inset to match those fixed size views at top.
  • Change height according to the scroll.

This is the way you can achieve the functionality you want. I hope it helps.

Related