Created
February 26, 2019 11:05
-
-
Save mecid/de52de9463a4e1a4982940c41fdc1dcb to your computer and use it in GitHub Desktop.
StackViewController
This file contains hidden or 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
import UIKit | |
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment