You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.2 KiB
Swift
72 lines
2.2 KiB
Swift
|
4 years ago
|
//
|
||
|
|
// VCEmbeder.swift
|
||
|
|
// Runner
|
||
|
|
//
|
||
|
|
// Created by Zohaib Iqbal Kambrani on 08/06/2021.
|
||
|
|
// Copyright © 2021 The Chromium Authors. All rights reserved.
|
||
|
|
//
|
||
|
|
|
||
|
|
import Foundation
|
||
|
|
|
||
|
|
extension UIView {
|
||
|
|
func fill(to parent: UIView) {
|
||
|
|
topAnchor.constraint(equalTo: parent.topAnchor).isActive = true
|
||
|
|
leadingAnchor.constraint(equalTo: parent.leadingAnchor).isActive = true
|
||
|
|
bottomAnchor.constraint(equalTo: parent.bottomAnchor).isActive = true
|
||
|
|
trailingAnchor.constraint(equalTo: parent.trailingAnchor).isActive = true
|
||
|
|
}
|
||
|
|
|
||
|
|
func fillToParent() {
|
||
|
|
if let parent = self.superview{
|
||
|
|
topAnchor.constraint(equalTo: parent.topAnchor).isActive = true
|
||
|
|
leadingAnchor.constraint(equalTo: parent.leadingAnchor).isActive = true
|
||
|
|
bottomAnchor.constraint(equalTo: parent.bottomAnchor).isActive = true
|
||
|
|
trailingAnchor.constraint(equalTo: parent.trailingAnchor).isActive = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func fillTo(view:UIView) {
|
||
|
|
view.addSubview(self)
|
||
|
|
fill(to: view)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class ViewEmbedder {
|
||
|
|
class func embed(
|
||
|
|
parent:UIViewController,
|
||
|
|
container:UIView,
|
||
|
|
child:UIViewController,
|
||
|
|
previous:UIViewController?){
|
||
|
|
|
||
|
|
if let previous = previous {
|
||
|
|
removeFromParent(vc: previous)
|
||
|
|
}
|
||
|
|
child.willMove(toParent: parent)
|
||
|
|
parent.addChild(child)
|
||
|
|
container.addSubview(child.view)
|
||
|
|
child.didMove(toParent: parent)
|
||
|
|
let w = container.frame.size.width;
|
||
|
|
let h = container.frame.size.height;
|
||
|
|
child.view.frame = CGRect(x: 0, y: 0, width: w, height: h)
|
||
|
|
|
||
|
|
child.view.fill(to: container)
|
||
|
|
}
|
||
|
|
|
||
|
|
class func removeFromParent(vc:UIViewController){
|
||
|
|
vc.willMove(toParent: nil)
|
||
|
|
vc.view.removeFromSuperview()
|
||
|
|
vc.removeFromParent()
|
||
|
|
}
|
||
|
|
|
||
|
|
class func embed(withIdentifier id:String, parent:UIViewController, container:UIView, completion:((UIViewController)->Void)? = nil){
|
||
|
|
let vc = parent.storyboard!.instantiateViewController(withIdentifier: id)
|
||
|
|
embed(
|
||
|
|
parent: parent,
|
||
|
|
container: container,
|
||
|
|
child: vc,
|
||
|
|
previous: parent.children.first
|
||
|
|
)
|
||
|
|
completion?(vc)
|
||
|
|
}
|
||
|
|
}
|