Skip to content

Instantly share code, notes, and snippets.

@DonMag
Last active March 27, 2020 15:45
Show Gist options
  • Save DonMag/220cb07468ca8699be056a1f6d5719b7 to your computer and use it in GitHub Desktop.
Save DonMag/220cb07468ca8699be056a1f6d5719b7 to your computer and use it in GitHub Desktop.
class TraitView: UIView {
let c: [UIColor] = [.red, .green, .blue]
var i = 0
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
print("trait changed \(i)")
super.traitCollectionDidChange(previousTraitCollection)
layer.backgroundColor = c[i % 3].cgColor
i = i + 1
}
}
class PresentTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton()
btn.setTitle("Tap Me", for: .normal)
btn.backgroundColor = .lightGray
btn.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(btn)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
btn.widthAnchor.constraint(equalToConstant: 200.0),
btn.centerXAnchor.constraint(equalTo: g.centerXAnchor, constant: 0.0),
])
btn.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
}
@objc func didTap(_ sender: Any?) -> Void {
let vc = TraitTestViewController()
present(vc, animated: true, completion: nil)
}
}
class TraitTestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemTeal
tableView.dataSource = self
tableView.delegate = self
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
let fv = TraitView(frame: CGRect(x: 0, y: 0, width: 200, height: 60))
tableView.tableFooterView = fv
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: g.topAnchor, constant: 100.0),
tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -40.0),
tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
])
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "bCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "bCell", for: indexPath)
c.textLabel?.text = "\(indexPath)"
return c
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment