Skip to content

Instantly share code, notes, and snippets.

@FahimF
Last active October 19, 2020 14:23
Show Gist options
  • Save FahimF/37182ef5ea3e3ca63fb35d87b186ee70 to your computer and use it in GitHub Desktop.
Save FahimF/37182ef5ea3e3ca63fb35d87b186ee70 to your computer and use it in GitHub Desktop.
A UIStackView implementation of UIListContentConfiguration and UIListContentView
import UIKit
class SimpleStackView: UIViewController {
private var scrollView: UIScrollView!
private var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// View title
title = "Books - Stack"
view.backgroundColor = UIColor.white
// Add scroll view
scrollView = UIScrollView(frame: view.bounds)
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(scrollView)
// Add stack view
stackView = UIStackView(frame: scrollView.bounds)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 8
stackView.distribution = .equalSpacing
scrollView.addSubview(stackView)
// Stack view constraings
stackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 16).isActive = true
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16).isActive = true
stackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -16).isActive = true
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -16).isActive = true
// Load data
loadData()
}
private func loadData() {
// Clear up any previous child views
for sv in stackView.subviews {
sv.removeFromSuperview()
}
// Add new child views for content - sections
for (index, section) in Book.sections.enumerated() {
// Section configuration
var config = UIListContentConfiguration.plainHeader()
config.text = section
// Section view
var cv = UIListContentView(configuration: config)
stackView.addArrangedSubview(cv)
// Loop through Books for section
for book in Book.booksFor(section: index) {
// Book configuration
config = UIListContentConfiguration.cell()
config.image = book.authType == .single ? UIImage(systemName: "person.fill") : UIImage(systemName: "person.2.fill")
config.text = book.title
config.secondaryText = book.author
// Book view
cv = UIListContentView(configuration: config)
stackView.addArrangedSubview(cv)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment