2

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.

2
  • 1
    Did you check this one stackoverflow.com/questions/1938921/… ? I think it would be easier for you to achieve it with tableView check this link medium.com/ios-os-x-development/… Commented Jun 12, 2019 at 7:56
  • 1
    Thanks for the links.. I have to use a container view for the navigation. There is no way around it. I achieved it with a table view tho :/ Commented Jun 12, 2019 at 8:08

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I made a gif to show the problem I am facing. Im not using a table view in the first place. I use a container view to navigate and then I add a table view into it.
is it a problem to use a container view here instead of a table view?
The code will work for any scrollView and not just the tableView.
Is your containerView scrolling?
Do I have to add a Scroll View element into my hierarchy?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.