Skip to content

Instantly share code, notes, and snippets.

@ohtwo
Last active November 18, 2015 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohtwo/83b4c6ef64eacc6ca0c9 to your computer and use it in GitHub Desktop.
Save ohtwo/83b4c6ef64eacc6ca0c9 to your computer and use it in GitHub Desktop.
Resizable Table Footer View
/*
* Solution - https://github.com/daveanderson/TableViewHeader
*/
class DynamicTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func updateViewConstraints() {
super.updateViewConstraints()
sizeFooterToFit()
}
private func sizeFooterToFit() {
// 1
let footerView = tableView.tableFooterView!
footerView.translatesAutoresizingMaskIntoConstraints = false
// 1.5
let temporaryWidthConstraints = NSLayoutConstraint.constraintsWithVisualFormat("[footerView(width)]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: ["width": footerView.frame.width],
views: ["footerView": footerView])
footerView.addConstraints(temporaryWidthConstraints)
// 2
footerView.setNeedsLayout()
footerView.layoutIfNeeded()
let size = footerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
var frame = footerView.frame
frame.origin = CGPointZero
frame.size = size
footerView.frame = frame
tableView.tableFooterView = footerView
// 3.5
footerView.removeConstraints(temporaryWidthConstraints)
// 4
footerView.translatesAutoresizingMaskIntoConstraints = true
}
}
class StaticTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment