Created
March 26, 2019 16:11
-
-
Save florianldt/3cd0de5464599505100b6a43f723a27c to your computer and use it in GitHub Desktop.
Possible solution for - Setting the stack view in a tableHeaderView #44
This file contains 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
// | |
// CustomView.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 1/3/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
class CustomView: UIView { | |
let label: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.isUserInteractionEnabled = false | |
label.font = UIFont.boldSystemFont(ofSize: 20) | |
return label | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: .zero) | |
self.backgroundColor = UIColor.random() | |
self.addSubview(label) | |
NSLayoutConstraint.activate([ | |
label.centerXAnchor.constraint(equalTo: self.centerXAnchor), | |
label.centerYAnchor.constraint(equalTo: self.centerYAnchor), | |
]) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
This file contains 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
// | |
// TableViewController.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 3/27/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
import AloeStackView | |
class TableViewController: UITableViewController { | |
let aloeStackView: AloeStackView = { | |
let sv = AloeStackView() | |
sv.backgroundColor = .red | |
sv.frame.size.height = 250 | |
return sv | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
setupTableView() | |
setupAloeStackView() | |
} | |
private func setupTableView() { | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.tableHeaderView = aloeStackView | |
} | |
private func setupAloeStackView() { | |
let view200 = CustomView() | |
view200.backgroundColor = UIColor.green | |
view200.heightAnchor.constraint(equalToConstant: 200).isActive = true | |
view200.label.text = "200" | |
aloeStackView.addRow(view200) | |
let view100 = CustomView() | |
view100.backgroundColor = UIColor.green | |
view100.heightAnchor.constraint(equalToConstant: 200).isActive = true | |
view100.label.text = "100" | |
aloeStackView.addRow(view100) | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 5 | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell() | |
cell.textLabel?.text = "\(indexPath)" | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment