I need to do this app that has a weird configuration.
As shown in the next image, the main view is a UIScrollView. Then inside it should have a UIPageView, and each page of the PageView should have a UITableView.
I've done all this so far. But my problem is that I want the scrolling to behave naturally.
The next is what I mean naturally. Currently when I scroll on one of the UITableViews, it scrolls the tableview (not the scrollview). But I want it to scroll the ScrollView unless the scrollview cannot scroll cause it got to its top or bottom (In that case I'd like it to scroll the tableview).
For example, let's say my scrollview is currently scrolled to the top. Then I put my finger over the tableview (of the current page being shown) and start scrolling down. I this case, I want the scrollview to scroll (no the tableview). If I keep scrolling down my scrollview and it reaches the bottom, if I remove my finger from the display and put it back over the tebleview and scroll down again, I want my tableview to scroll down now because the scrollview reached its bottom and it's not able to keep scrolling.
Do you guys have any idea about how to implement this scrolling?
I'm REALLY lost with this. Any help will be greatly appreciate it :(
Thanks!
Modified Daniel's answer to make it more efficient and bug free.
Complete project can be seen here: https://gitlab.com/vineetks/TableScroll.git
I found an awesome library MXParallaxHeader
In Storyboard just set
UIScrollView
class toMXScrollView
then magic happens.I used this class to handle my
UIScrollView
when I embed aUIPageViewController
container view. even you can insert a parallax header view for more detail.Also, this library provides
Cocoapods
andCarthage
I attached an image below which represent
UIView
Hierarchy. MXScrollView HierarchyI tried the solution marked as the correct answer, but it was not working properly. The user need to click two times on the table view for scroll and after that I was not able to scroll the entire screen again. So I just applied the following code in viewDidLoad():
And the code below is the implementation of the actions:
After many trials and errors, this is what worked best for me. The solution has to solve two needs 1) determine who's scrolling property should be used; tableView or scrollView? 2) make sure that the tableView doesn't give authority to the scrollView until it has reached the top of it's table/content.
In order to see if the scrollview should be used for scrolling vs the tableview, i checked to see if the UIView right above my tableview was within frame. If the UIView is within frame, it's safe to say the scrollView should have authority to scroll. If the UIView is not within frame, that means that the tableView is taking up the entire window, and therefor should have authority to scroll.
hope this helps someone!
I think there are two options.
Since you know the size of the scroll view and the main view, you are unable to tell whether the scroll view hit the bottom or not.
So when it hit; you basically set
and other way around for your tableView.
The other thing, which is more precise I think, is to add Gesture to your views.
So when you add Gesture, you can simply control the active view by changing
setScrollEnabled
in therespondToTapGesture
.The solution to simultaneously handling the scroll view and the table view revolves around the
UIScrollViewDelegate
. Therefore, have your view controller conform to that protocol:I’ll represent the scroll view and table view as outlets:
We’ll also need to track the height of the scroll view content as well as the screen height. You’ll see why later.
A little configuration is needed in
viewDidLoad:
:where I’ve turned off bouncing to keep things simple. The key settings are the delegates for the scroll view and the table view and having the table view scrolling being turned off at first.
These are necessary so that the
scrollViewDidScroll:
delegate method can handle reaching the bottom of the scroll view and reaching the top of the table view. Here is that method:What the delegate method is doing is detecting when the scroll view has reached its bottom. When that has happened the table view can be scrolled. It is also detecting when the table view reaches the top where the scroll view is re-enabled.
I created a GIF to demonstrate the results: