Skip to content

Instantly share code, notes, and snippets.

@alonecuzzo
Created November 16, 2015 23:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alonecuzzo/657067df0839cf4be30a to your computer and use it in GitHub Desktop.
Save alonecuzzo/657067df0839cf4be30a 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 hedder = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.tableView)
self.tableView.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(self.view)
}
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()
hedder.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.tableView)
make.width.equalTo(self.view)
make.height.equalTo(headerHeight)
make.centerX.equalTo(self.view)
}
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return headerHeight
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
hedder.backgroundColor = UIColor.purpleColor()
return hedder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment