Skip to content

Instantly share code, notes, and snippets.

@d-date
Created August 19, 2019 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-date/294d4c791a273a502a68027a3a07d9f8 to your computer and use it in GitHub Desktop.
Save d-date/294d4c791a273a502a68027a3a07d9f8 to your computer and use it in GitHub Desktop.
import UIKit
open class VStackViewController: UIViewController {
public let scrollView: UIScrollView = .init()
public let stackView: UIStackView = {
let stackView: UIStackView = .init()
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .equalSpacing
stackView.spacing = 0
return stackView
}()
var components: [UIViewController]
public init(components: [UIViewController]) {
self.components = components
super.init(nibName: nil, bundle: nil)
}
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override open func viewDidLoad() {
super.viewDidLoad()
components.forEach { [weak self] in self?.addChild($0) }
view.addSubview(scrollView, constraints: .allEdges())
scrollView.addSubview(stackView, constraints: .allEdges() + [equal(\.widthAnchor)])
components.forEach { [weak self] in
guard let self = self else { return }
self.stackView.addArrangedSubview($0.view)
}
components.forEach { [weak self] in
guard let self = self else { return }
$0.didMove(toParent: self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment