Search code examples
iosswiftuitableviewuiscrollviewuinavigationbar

UITableView on scrolling up moves from front to behind


Below view is a tableViewController which is implemented and set the delegate and datasource methods and it works perfectly fine but I have no idea how to implement the purple view behavior on scrolling up and down.

How can I implement it?

The below code is the way I detect that view scrolled up or down. but still, no clue to implement this behavior.

override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
    if translation.y > 0 {
        presenter?.viewScrolledDown()
    } else {
        presenter?.viewScrolledUp()
    }
}

scrolling-view-up


Solution

  • this question covers how to animate a backgroundColor. If the purple view is a UIView, this won't be a problem. It starts behind the tableView and then moves to the front of the tableView. You can handle this by using these functions:

    view.bringSubviewToFront(purpleView)
    view.sendSubviewToBack(tableView)
    

    You will also need to animate the autoLayout constraints to move the bottom of the purple view up to the top of the tableView.