Skip to content

Instantly share code, notes, and snippets.

@johndpope
Forked from alonecuzzo/Header.swift
Last active May 22, 2017 17:58
Show Gist options
  • Save johndpope/7f67154f11ccfdd92554bf989bcad61c to your computer and use it in GitHub Desktop.
Save johndpope/7f67154f11ccfdd92554bf989bcad61c to your computer and use it in GitHub Desktop.
Get HeaderView Sized with SnapKit
//
// ViewController.swift
// SnapKitTest
//
// Created by Robert Payne on 10/11/15.
// Copyright © 2015 Zwopple Limited. All rights reserved.
//
import SnapKit
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
let tableView = UITableView()
let headerHeight: CGFloat = 75
let headerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.tableView)
self.tableView.snp_makeConstraints { (make) -> Void in
make.edges.equalToSuperView()
}
self.tableView.delegate = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//need to force redraw here, then hedder is guaranteed to be attached to tableView
tableView.setNeedsLayout()
tableView.layoutIfNeeded()
headerView.snp_makeConstraints { (make) -> Void in
make.top.left.width.equalToSuperview()
make.height.equalTo(headerHeight)
}
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return headerHeight
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
hedder.backgroundColor = UIColor.purpleColor()
return headerView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment