Created
September 4, 2019 11:34
-
-
Save Edudjr/49f65f9aa3ae63b5c658eb055790c2df to your computer and use it in GitHub Desktop.
This is an example on how to create a view controller stack in swift.
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 ViewControllerStack: UIViewController { | |
private var firstContainerTopAnchor: NSLayoutConstraint? | |
private var firstController: UIViewController? | |
private var controllers: [UIViewController]? | |
private var containerView = UIView() | |
init(controllers: [UIViewController]) { | |
self.controllers = controllers | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder _: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .purple | |
containerView.backgroundColor = .gray | |
containerView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(containerView) | |
addConstraints(to: containerView, parent: view) | |
if let controllers = controllers { | |
for controller in controllers { | |
addToContainer(viewController: controller, container: containerView) | |
} | |
} | |
} | |
private func addConstraints(to childView: UIView, parent: UIView) { | |
NSLayoutConstraint.activate([ | |
childView.topAnchor.constraint(equalTo: parent.topAnchor), | |
childView.bottomAnchor.constraint(equalTo: parent.bottomAnchor), | |
childView.leadingAnchor.constraint(equalTo: parent.leadingAnchor), | |
childView.trailingAnchor.constraint(equalTo: parent.trailingAnchor), | |
]) | |
} | |
private func sumHeight(_ height: CGFloat) { | |
preferredContentSize.height += height | |
} | |
func addToContainer(viewController: UIViewController, container: UIView) { | |
addChild(viewController) | |
viewController.view.translatesAutoresizingMaskIntoConstraints = false | |
container.addSubview(viewController.view) | |
// Set initial constraints for viewController | |
NSLayoutConstraint.activate([ | |
viewController.view.leadingAnchor.constraint(equalTo: container.leadingAnchor), | |
viewController.view.heightAnchor.constraint(equalToConstant: viewController.preferredContentSize.height), | |
viewController.view.trailingAnchor.constraint(equalTo: container.trailingAnchor) | |
]) | |
if let firstController = firstController { | |
firstContainerTopAnchor?.isActive = false | |
firstContainerTopAnchor = nil | |
firstController.view.topAnchor.constraint(equalTo: viewController.view.bottomAnchor).isActive = true | |
} else { | |
viewController.view.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true | |
} | |
firstContainerTopAnchor = viewController.view.topAnchor.constraint(equalTo: container.topAnchor) | |
firstContainerTopAnchor?.isActive = true | |
firstController = viewController | |
viewController.didMove(toParent: self) | |
sumHeight(viewController.preferredContentSize.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment