Last active
October 27, 2019 16:04
-
-
Save smosko/12294d2142e0f67df3c2d807e42fd772 to your computer and use it in GitHub Desktop.
Building complex screens with child ViewControllers
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
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
https://mecid.github.io/2019/02/27/building-complex-screens-with-child-viewcontrollers/