Example of how to use AutoLayout with table header view.
It comes with UITableView's extension that helps you do this easily.
There are two examples:
- using UITableView (with header view Xib)
- using UITableViewController (all in storyboard).
Requirements: Xcode 8, Swift 3, iOS 9+ (use anchors)
More detail here
- In viewDidLoad()
// 1. Setup AutoLayout (do this instead of setting constraints via storyboard)
self.tableView.setTableHeaderView(headerView: self.tableView.tableHeaderView!)
// 2. Initial layout update
self.tableView.updateHeaderViewFrame()
- (If you want to handle device rotation) In viewWillTransition(to size: with coordinator:)
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// 3. Update layout every time device is rotated.
DispatchQueue.main.async {
self.tableView.updateHeaderViewFrame()
}
}