Created
February 2, 2023 15:10
-
-
Save DonMag/041d243df1c9d4b53a1a6f923ce13ecd to your computer and use it in GitHub Desktop.
Simple Swift example of adding Child View Controllers
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
class ViewController: UIViewController { | |
let infoLabel = UILabel() | |
let stack = UIStackView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .systemYellow | |
infoLabel.textAlignment = .center | |
infoLabel.text = "Tap anywhere to add children..." | |
stack.axis = .vertical | |
stack.distribution = .fillEqually | |
stack.spacing = 20.0 | |
infoLabel.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(infoLabel) | |
stack.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(stack) | |
let g = view.safeAreaLayoutGuide | |
NSLayoutConstraint.activate([ | |
infoLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0), | |
infoLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0), | |
infoLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0), | |
stack.topAnchor.constraint(equalTo: infoLabel.bottomAnchor, constant: 20.0), | |
stack.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0), | |
stack.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0), | |
stack.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0), | |
]) | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
// we only want to add the child controllers once | |
if self.children.count > 0 { return() } | |
let colors: [UIColor] = [ | |
.systemRed, .systemGreen, .systemBlue, | |
] | |
for (i, c) in colors.enumerated() { | |
let vc = SimpleChildVC() | |
addChild(vc) | |
vc.text = "Child \(i)" | |
vc.view.backgroundColor = c | |
stack.addArrangedSubview(vc.view) | |
vc.didMove(toParent: self) | |
} | |
infoLabel.text = "Three child view controllers added!" | |
} | |
} | |
class SimpleChildVC: UIViewController { | |
public var text: String = "" { | |
didSet { | |
label.text = text | |
} | |
} | |
private let label = UILabel() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
label.backgroundColor = UIColor(white: 0.95, alpha: 1.0) | |
label.textAlignment = .center | |
label.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(label) | |
let g = view.safeAreaLayoutGuide | |
NSLayoutConstraint.activate([ | |
label.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.8), | |
label.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 0.6), | |
label.centerXAnchor.constraint(equalTo: g.centerXAnchor), | |
label.centerYAnchor.constraint(equalTo: g.centerYAnchor), | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment