Skip to content

Instantly share code, notes, and snippets.

@thomsmed
Last active August 22, 2022 19:53
Show Gist options
  • Select an option

  • Save thomsmed/f70ad08ac8c8a8fd1e555db72647121f to your computer and use it in GitHub Desktop.

Select an option

Save thomsmed/f70ad08ac8c8a8fd1e555db72647121f to your computer and use it in GitHub Desktop.
TableViewController.swift - From "Expandable and dynamic sized Table Header View and Table Footer View" @ medium.com
//
// TableViewController.swift
//
import UIKit
// ...
final class TableViewController: UITableViewController {
// ...
private let headerView = TableHeaderView()
private let footerView = TableFooterView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableHeaderView = headerView
tableView.tableFooterView = footerView
headerView.addGestureRecognizer(
UITapGestureRecognizer(
target: self,
action: #selector(toggleExpandedHeaderView)
)
)
// ...
tableView.layoutIfNeeded() // To make sure the UITableView's layout includes an up to date TableHeaderView.
}
@objc private func toggleExpandedHeaderView() {
tableView.performBatchUpdates {
let oldHeaderHeight = headerView.frame.height
// 0.3 is an educated guess about the duration of UITableView's own update animation duration.
UIView.animate(withDuration: 0.3) {
self.headerView.expanded = !self.headerView.expanded
self.tableView.layoutIfNeeded()
let newHeaderHeight = self.headerView.frame.height
// Animate the footer's center along the header's size change.
self.footerView.center.y += newHeaderHeight - oldHeaderHeight
}
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment