how can I manage controllers in Container View with using tab bar

后端 未结 1 862
一向
一向 2021-01-27 17:10

On my storyboard I have main ViewController, not TabBarViewController, which consist of TabBar on the bottom, view on the top and ContainerView

相关标签:
1条回答
  • 2021-01-27 17:43

    You can try to use this extesion to add/remove any of the 4 to containerView

    extension UIViewController {
        func add(_ child: UIViewController, frame: CGRect? = nil) {
            addChildViewController(child)
            if let frame = frame {
                child.view.frame = frame
            }
            view.addSubview(child.view)
            child.didMove(toParentViewController: self)
        }
        func remove() {
            willMove(toParentViewController: nil)
            view.removeFromSuperview()
            removeFromParentViewController()
    
       }
    
    }
    

    // use it like this

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")
    
    self.add(vc, frame: self.containerView.frame)
    

    to remove

    vc.remove()
    
    0 讨论(0)
提交回复
热议问题