Created
July 26, 2017 04:38
-
-
Save AntonTheDev/a1ae562234c55e451b885b16871318ac to your computer and use it in GitHub Desktop.
StackoverFlow Example
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
| https://stackoverflow.com/questions/45250787/ios-correctly-adopting-a-view-into-an-already-visible-parent | |
| extension UIViewController { | |
| func adopChilViewController(_ childViewController: UIViewController) { | |
| addChildViewController(childViewController) | |
| view.addSubview(childViewController.view) | |
| childViewController.didMove(toParentViewController: self) | |
| } | |
| } | |
| class ViewController1 : UIViewController { | |
| let vc2 = ViewController2() | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| view.addSubview(addChildButton) | |
| view.addSubview(pushButton) | |
| addChildButton.frame = CGRect(x: 0.0, y: 64, | |
| width: view.bounds.width, height: 100) | |
| pushButton.frame = CGRect(x: 0.0, y: 164, | |
| width: view.bounds.width, height:100) | |
| } | |
| func addVC2() { | |
| adopChilViewController(vc2) | |
| vc2.view.backgroundColor = UIColor.yellow | |
| vc2.view.frame = CGRect(x: 0.0, y: 264, | |
| width: view.bounds.width, height: 100) | |
| } | |
| func pushVC3() { | |
| navigationController?.pushViewController(ViewController3(), animated: true) | |
| } | |
| lazy var addChildButton : UIButton = { | |
| let button = self.newButtonWith(title: "Add Child (ViewController2)", | |
| selector : #selector(ViewController1.addVC2)) | |
| return button | |
| }() | |
| lazy var pushButton : UIButton = { | |
| let button = self.newButtonWith(title: "Push View (ViewController3)", | |
| selector : #selector(ViewController1.pushVC3)) | |
| return button | |
| }() | |
| } | |
| class ViewController2 : UIViewController { | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| print("ViewController2 viewWillAppear") | |
| } | |
| override func viewDidAppear(_ animated: Bool) { | |
| super.viewDidAppear(animated) | |
| print("ViewController2 viewDidAppear") | |
| } | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| print("ViewController2 viewWillDisappear") | |
| } | |
| override func viewDidDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| print("ViewController2 viewDidDisappear") | |
| } | |
| } | |
| class ViewController3 : UIViewController { | |
| override func viewWillAppear(_ animated: Bool) { | |
| view.addSubview(popViewControllerButton) | |
| popViewControllerButton.frame = CGRect(x: 0.0, y: 64, width: view.bounds.width, height: 100) | |
| } | |
| lazy var popViewControllerButton : UIButton = { | |
| let button = self.newButtonWith(title: "Pop Current ViewController3", | |
| selector : #selector(ViewController3.popVC3)) | |
| return button | |
| }() | |
| func popVC3() { | |
| self.navigationController?.popViewController(animated: true) | |
| } | |
| } | |
| extension UIViewController { | |
| func newButtonWith(title : String, selector : Selector) -> UIButton { | |
| let button = UIButton(type: .custom) | |
| button.backgroundColor = UIColor.black | |
| button.setTitle(title, for: .normal) | |
| button.addTarget(self, action: selector, for: .touchUpInside) | |
| return button | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment