Move a view when scrolling in UITableView Move a view when scrolling in UITableView ios ios

Move a view when scrolling in UITableView


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


More simple and fast approach

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {    CGRect rect = self.view.frame;    rect.origin.y =  -scrollView.contentOffset.y;    self.view.frame = rect;}


matomo