Alan
Alan

Reputation: 9471

Shrinking UITableView Header creates a gap between the header and cells

I have a UITableView with a header that I am shrinking as I scroll down, but it is creating a gap between the header and the cells.

Current code:

func moveLogoBarUp(_ scrollView: UIScrollView) {
        self.tableView.bounces = true

        let scrollDiff = self.tableView.contentOffset.y - self.previousScrollOffset

        let newHeight = (self.tableView.tableHeaderView?.frame.height)! - abs(scrollDiff)

        if newHeight <= self.tableHeaderCollapsedHeight {
            self.tableView.tableHeaderView?.frame.size.height = self.tableHeaderCollapsedHeight
        } else {
            self.logoBarTopConstraint.constant -= abs(scrollDiff)
            self.tableView.tableHeaderView?.frame.size.height = newHeight
            self.tableView.contentOffset = CGPoint(x: self.tableView.contentOffset.x, y: self.previousScrollOffset)

            self.previousScrollOffset = scrollView.contentOffset.y
            //self.tableView.reloadData()
        }
    }

Note: If I use self.tableView.reloadData() it will fix the issue, but this doesn't seem ideal as it calls the method very rapidly as it scrolls.

How do I make the cells go up as the header shrinks?

Upvotes: 1

Views: 204

Answers (1)

Dima
Dima

Reputation: 23624

The table view should recalculate the layout for the header if you set the property again. Something like this should do it:

self.tableView.tableHeaderView = self.tableView.tableHeaderView

Upvotes: 2

Related Questions