pajevic pajevic - 5 months ago 100
iOS Question

Move a view when scrolling in UITableView

I have a

UIView
with a
UITableView
below it:

enter image description here

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.

Answer

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;
}
Comments