I have a UIView
with a UITableView
below it:
What I would like to do is to have the view above the UITableView
move up (out of the way) when the user starts scrolling in the table in order to have more space for the UITableView
(and come down when you scroll down again).
I know that this is normally done with a table header view, but my problem is that my table view is inside a tab (actually it is a side-scrolling page view implemented using TTSliddingPageviewcontroller
). So while I only have one top UIView
there are three UITableView
s.
Is it possible to accomplish this manually? My first thought is to put everything in a UIScrollView
, but according to Apple's documentation one should never place a UITableView
inside a UIScrollView
as this leads to unpredictable behavior.
Solution for Swift (Works perfectly with bounce enabled for scroll view):
I added some constraints to the last solution to prevent some strange behaviours in case of fast scrolling
Someone asked for the code for my solution so I am posting it here as an answer. The credit for the idea should still go to NobodyNada.
In my
UITableViewController
I implement this delegate method:scrollUserInfo
is aNSDictionary
where I put myUITableView
to pass it with the notification (I do this inviewDidLoad
so I only have to do it once):Now, in the view controller that has the view I want to move off screen while scrolling I do this in
viewDidLoad
:And finally I have the method:
previousOffset
is an instance variableCGFloat previousOffset;
.topVerticalConstraint
is aNSLayoutConstraint
that is set as aIBOutlet
. It goes from the top of the view to the top of its superview and the initial value is 0.It's not perfect. For instance, if the user scrolls very aggressively up the movement of the view can get a bit jerky. The issue is worse for large views; if the view is small enough there is no problem.
I know this post in very old. I tried above solutions but neither worked for me for tried my own, hopefully it can help you. This scenario is pretty common, as apple suggested not to use TableViewController inside any ScrollView because the compiler will confused as in whom to respond becuase it will be getting two delegate call back - one from ScrollViewDelegate and another from UITableViewDelegate.
Instead we can use ScrollViewDelegate and disable the UITableViewScrolling.
Here lastContentOffset is CGFloat defined as property
The View Heirarchy is as follows: ViewController --> View contains ScrollView (whose delegate method is defined above) --> Contain TableView.
By the above code we are manually increasing and decreasing the height of the table view along with the content size of ScrollView.
Remember to disable the Scrolling of TableView.
More simple and fast approach
To create like this animation,