Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ViewController+Containment.swift | ||
// Tempura | ||
// | ||
// Created by Andrea De Angelis on 25/10/2018. | ||
// | ||
|
||
|
||
/// ViewController containment | ||
extension ViewController { | ||
/// Add a ViewController (and its rootView) as a child of self | ||
/// You must provide a ContainerView inside self.rootView to receive the rootView of the child ViewController | ||
public func add<View: ViewControllerModellableView>(_ child: ViewController<View>, in view: ContainerView) { | ||
self.addChild(child) | ||
view.addSubview(child.rootView) | ||
child.didMove(toParent: self) | ||
} | ||
|
||
/// Remove a child ViewController | ||
public func remove() { | ||
guard let _ = parent else { return } | ||
self.willMove(toParent: nil) | ||
self.removeFromParent() | ||
self.rootView.removeFromSuperview() | ||
} | ||
} | ||
|
||
/// A View used to do ViewController containment | ||
/// This is the View that will contain the View of the managed ViewController | ||
public class ContainerView: UIView { | ||
|
||
public override func layoutSubviews() { | ||
super.layoutSubviews() | ||
self.subviews.forEach { | ||
$0.frame = self.bounds | ||
} | ||
} | ||
} |
This file contains 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