Skip to content
Example of how to use AutoLayout with table header view.
Swift
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
TableHeaderViewWithAutoLayout.xcodeproj Remove redundant code Jun 28, 2017
TableHeaderViewWithAutoLayout
README.md Update README.md Jun 28, 2017

README.md

TableHeaderViewWithAutoLayout

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:

  1. using UITableView (with header view Xib)
  2. using UITableViewController (all in storyboard).

Requirements: Xcode 8, Swift 3, iOS 9+ (use anchors)

More detail here

alt gif

How to use

  1. 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()
  1. (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()
    }
}
You can’t perform that action at this time.