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.
Here is how you can handle the
headerView
height
withtableView
scrolling
,In the above code
headerView
will,tableView
isscrolled up
tableView
isscrolled down
Let me know in case you still face any issues.