Last active
November 18, 2015 02:00
-
-
Save ohtwo/83b4c6ef64eacc6ca0c9 to your computer and use it in GitHub Desktop.
Resizable Table Footer View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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