Created
November 16, 2015 23:47
-
-
Save alonecuzzo/657067df0839cf4be30a to your computer and use it in GitHub Desktop.
Get HeaderView Sized with SnapKit
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
// | |
// 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