Search code examples
iosswiftuiscrollviewuiscrollviewdelegate

How to implement a collapsing header?


I tried to implement a collapsing UITableView Header but right now I am not getting any further. This is my storyboard. I tried to use the scrollViewDidScroll(scrollView: UIScrollView) delegate method but the position is not changing at all while scrolling in the table view which is embedded in the container. The heightConstraint is the height of my container header view. Here is the full source code for my class. I appreciate any help! Sitting on this problem for some time now.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.heightConstraint.constant = self.maxHeaderHeight
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let absoluteTop: CGFloat = 0
    let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height
    let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset
    let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
    let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom

    var newHeight = self.heightConstraint.constant
    if isScrollingDown {
        newHeight = max(self.minHeaderHeight, self.heightConstraint.constant - abs(scrollDiff))
    } else if isScrollingUp {
        newHeight = min(self.maxHeaderHeight, self.heightConstraint.constant + abs(scrollDiff))
    }

    if newHeight != self.heightConstraint.constant {
        self.heightConstraint.constant = newHeight
    }

    self.previousScrollOffset = scrollView.contentOffset.y
}

While scrolling up the header container should disappear/change its location.


Solution

  • Here is how you can handle the headerView height with tableView scrolling,

    class VC: UIViewController {
        @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    
        var lastContentOffset: CGFloat = 0.0
        let maxHeaderHeight: CGFloat = 115.0
    
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
                //Scrolled to bottom
                UIView.animate(withDuration: 0.3) {
                    self.heightConstraint.constant = 0.0
                    self.view.layoutIfNeeded()
                }
            }
            else if (scrollView.contentOffset.y < self.lastContentOffset || scrollView.contentOffset.y <= 0) && (self.heightConstraint.constant != self.maxHeaderHeight)  {
                //Scrolling up, scrolled to top
                UIView.animate(withDuration: 0.3) {
                    self.heightConstraint.constant = self.maxHeaderHeight
                    self.view.layoutIfNeeded()
                }
            }
            else if (scrollView.contentOffset.y > self.lastContentOffset) && self.heightConstraint.constant != 0.0 {
                //Scrolling down
                UIView.animate(withDuration: 0.3) {
                    self.heightConstraint.constant = 0.0
                    self.view.layoutIfNeeded()
                }
            }
            self.lastContentOffset = scrollView.contentOffset.y
        }
    }
    

    In the above code headerView will,

    1. collapse when the tableView is scrolled up
    2. expand when the tableView is scrolled down

    Let me know in case you still face any issues.