Created
January 29, 2021 15:36
-
-
Save Edudjr/07223c2d4bac8c6f997f53fc2a85f6d3 to your computer and use it in GitHub Desktop.
Example of ViewController container
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 | |
import PlaygroundSupport | |
let screenHeight: CGFloat = 400 | |
let screenWidth: CGFloat = 200 | |
class InnerViewController: UIViewController { | |
var stack: UIStackView = { | |
var label: UILabel { | |
let label = UILabel() | |
label.text = "testing" | |
return label | |
} | |
let stack = UIStackView() | |
stack.axis = .vertical | |
stack.backgroundColor = .red | |
stack.addArrangedSubview(label) | |
stack.addArrangedSubview(label) | |
stack.translatesAutoresizingMaskIntoConstraints = false | |
return stack | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .gray | |
view.addSubview(stack) | |
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
stack.topAnchor.constraint(equalTo: view.topAnchor).isActive = true | |
stack.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true | |
} | |
} | |
class ViewController: UIViewController { | |
var viewContainer: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .cyan | |
return view | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .gray | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
setupViewContainer() | |
addViewController(vc: InnerViewController()) | |
} | |
func setupViewContainer() { | |
view.addSubview(viewContainer) | |
viewContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
viewContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
viewContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true | |
viewContainer.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor).isActive = true | |
} | |
func addViewController(vc: UIViewController) { | |
vc.view.translatesAutoresizingMaskIntoConstraints = false | |
self.addChild(vc) | |
viewContainer.addSubview(vc.view) | |
vc.didMove(toParent: self) | |
vc.view.leadingAnchor.constraint(equalTo: viewContainer.leadingAnchor).isActive = true | |
vc.view.trailingAnchor.constraint(equalTo: viewContainer.trailingAnchor).isActive = true | |
vc.view.topAnchor.constraint(equalTo: viewContainer.topAnchor).isActive = true | |
vc.view.bottomAnchor.constraint(equalTo: viewContainer.bottomAnchor).isActive = true | |
} | |
} | |
let vc = ViewController() | |
vc.view.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight) | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
PlaygroundPage.current.liveView = vc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment