-
-
Save johndpope/7f67154f11ccfdd92554bf989bcad61c 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 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