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.
UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.
Solution for Swift (Works perfectly with bounce enabled for scroll view):
var oldContentOffset = CGPointZero let topConstraintRange = (CGFloat(120)..<CGFloat(300)) func scrollViewDidScroll(scrollView: UIScrollView) { let delta = scrollView.contentOffset.y - oldContentOffset.y //we compress the top view if delta > 0 && topConstraint.constant > topConstraintRange.start && scrollView.contentOffset.y > 0 { topConstraint.constant -= delta scrollView.contentOffset.y -= delta } //we expand the top view if delta < 0 && topConstraint.constant < topConstraintRange.end && scrollView.contentOffset.y < 0{ topConstraint.constant -= delta scrollView.contentOffset.y -= delta } oldContentOffset = scrollView.contentOffset }
Since UITableView
is a subclass of UIScrollView
, your table view's delegate can receive UIScrollViewDelegate
methods.
In your table view's delegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
static CGFloat previousOffset;
CGRect rect = self.view.frame;
rect.origin.y += previousOffset - scrollView.contentOffset.y;
previousOffset = scrollView.contentOffset.y;
self.view.frame = rect;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With