Skip to content

Instantly share code, notes, and snippets.

@smosko
Last active October 27, 2019 16:04
Show Gist options
  • Save smosko/12294d2142e0f67df3c2d807e42fd772 to your computer and use it in GitHub Desktop.
Save smosko/12294d2142e0f67df3c2d807e42fd772 to your computer and use it in GitHub Desktop.
Building complex screens with child ViewControllers
class StackViewController: UIViewController {
private let scrollView = UIScrollView()
private let stackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.addSubview(stackView)
setupConstraints()
stackView.axis = .vertical
}
private func setupConstraints() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
stackView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor)
])
}
}
extension StackViewController {
func add(_ child: UIViewController) {
addChild(child)
stackView.addArrangedSubview(child.view)
child.didMove(toParent: self)
}
func remove(_ child: UIViewController) {
guard child.parent != nil else {
return
}
child.willMove(toParent: nil)
stackView.removeArrangedSubview(child.view)
child.view.removeFromSuperview()
child.removeFromParent()
}
}
// Usage example:
class DayViewController: StackViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
private func setupUI() {
let today = Date()
let calendarVC = CalendarViewController(date: today)
calendarVC.delegate = self
add(calendarVC)
add(SummaryViewController(date: today))
add(SleepViewController(date: today))
add(WorkoutViewController(date: today))
}
}
extension DayViewController: CalendarViewControllDelegate {
func dateSelected(_ date: Date) {
reloadDay(with: date)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment