Search code examples
iosswiftuser-interfaceswift3xcode8

Pass Multiple ViewControllers through One Container View - Xcode 8, Swift 3


I simply would like to pass a Viewcontroller through the Container View when a Button is pressed. My navigation is in a container view as well and scrolls horizontally(like a dock) Here are 2 pictures to illustrate what I am trying to do. enter image description here

enter image description here

Is this possible with a ContainerView?


Solution

  • Figured it out!! I removed the blue Container View and swapped it for a Scroll View Here's the Code for anyone else who is interested in this. Still working on making toe Container View's height dynamic to the content.

    This tutorial helped a lot: Tutorial

    Main View Controller:

    var container: ContainerViewController!
    
    override func viewDidLoad() {
        container!.segueIdentifierReceivedFromParent("first")
    
    }
    
    @IBAction func firstBtnPressed(_ sender: Any) {
        let vc = "first"
         container!.segueIdentifierReceivedFromParent(vc)
    
    }
    
    @IBAction func secondBtnPressed(_ sender: Any) {
        container!.segueIdentifierReceivedFromParent("second")
    }
    
    @IBAction func thirdBtnPressed(_ sender: Any) {
        container!.segueIdentifierReceivedFromParent("third")
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "container"{
    
            container = segue.destination as! ContainerViewController
    
        }
    }
    

    Container View(Purple View)

    open class ContainerViewController: UIViewController {
        //Manipulating container views
        fileprivate weak var viewController : UIViewController!
        //Keeping track of containerViews
        fileprivate var containerViewObjects = Dictionary<String,UIViewController>()
    
        /** Specifies which ever container view is on the front */
        open var currentViewController : UIViewController{
            get {
                return self.viewController  
            }
        }
    
        fileprivate var segueIdentifier : String!
    
        /*Identifier For First Container SubView*/
        @IBInspectable internal var firstLinkedSubView : String!
    
        override open func viewDidLoad() {
            super.viewDidLoad()
        }
    
        open override func viewDidAppear(_ animated: Bool) {
            if let identifier = firstLinkedSubView{
                segueIdentifierReceivedFromParent(identifier)
            }
        }
    
        override open func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func segueIdentifierReceivedFromParent(_ identifier: String) {        
            self.segueIdentifier = identifier
            self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)    
        }
    
        override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == segueIdentifier {            
                //Remove Container View
                if viewController != nil {
                    viewController.view.removeFromSuperview()
                    viewController = nil
                }
                //Add to dictionary if isn't already there
                if ((self.containerViewObjects[self.segueIdentifier] == nil)) {
                    viewController = segue.destination
                    self.containerViewObjects[self.segueIdentifier] = viewController                
                } else {
                    for (key, value) in self.containerViewObjects {
                        if key == self.segueIdentifier {                        
                            viewController = value
                        }
                    }
                }
    
                self.addChildViewController(viewController)
                viewController.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
                self.view.addSubview(viewController.view)
                viewController.didMove(toParentViewController: self)
            }
        }
    }