Skip to content

Instantly share code, notes, and snippets.

@alexmx
Created April 7, 2017 13:30
Show Gist options
  • Save alexmx/3bd217b25542fc3dd41fa79cfe2a22c7 to your computer and use it in GitHub Desktop.
Save alexmx/3bd217b25542fc3dd41fa79cfe2a22c7 to your computer and use it in GitHub Desktop.
Adjust UITableView footer view to fill the whole remaining part of the screen.
@nathanhosselton
Copy link

nathanhosselton commented Aug 18, 2020

Prior solutions were additive to the footer height when viewDidLayoutSubviews is called multiple times, causing the footer to become larger than intended. Maybe it worked fine in a UITableView subclass/extension, but not in a UITableViewController (my usage).

Here's the updated logic applied directly in a UITableViewController context:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // Fill any empty content space with the footer view, pushing it to the bottom of the screen.
    fillContentGap:
    if let tableFooterView = tableView.tableFooterView {
        /// The expected height for the footer under autolayout.
        let footerHeight = tableFooterView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
        /// The amount of empty space to fill with the footer view.
        let gapHeight: CGFloat = tableView.bounds.height - tableView.adjustedContentInset.top - tableView.adjustedContentInset.bottom - tableView.contentSize.height
        // Ensure there is space to be filled
        guard gapHeight.rounded() > 0 else { break fillContentGap }
        // Fill the gap
        tableFooterView.frame.size.height = gapHeight + footerHeight
    }
}

Of course, make sure your constraints on your footer content are sufficient to keep it pinned to the view's bottom edge as its height expands.

It's also worth noting that the Swift compiler has a time with the lengthy gapHeight calculation, which is why I explicitly declared its type. In my usage I have an extension var vertical: CGFloat { top + bottom } on UIEdgeInsets which helps reduce this line a bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment