Manually making a Container View

2250 views ios
-1

I know Container views are just a class of Views. I want to make a container view that is scrollable, so I don't want to be anchored by the default size that the new view controller is forced as.
I wanted to instantiate a ViewController (of its own class) and set it up in the View I already have in my storyboard (instead of programmatically making a rectangle and stuff). The biggest reason for this is that I want to have some delegates to pass information between the main view and the sub-view

    class HomeVC: UIViewController {
        @IBOutlet weak var subView: UIView! //This one is linked to the Storyboard
        func subViewInit() {
            let resultsVC = self.storyboard?.instantiateViewController(withIdentifier: "resultsView") as! GoalsVC
            resultsVC.goalDelegate = self
            // NOW HOW DO I INSERT THIS INTO MY SUBVIEW??
        }
}

Maybe I'm taking the wrong approach and overcomplicating this?

answered question

It's unclear what you're asking. You can't insert a view controller into a view. You can only insert views into other views. Also, you can set up the delegate in Interface Builder if you want to.

1 Answer

3

Seems like a better approach. You can add that ViewController's view to subView as below,

guard let resultsVC = self.storyboard?.instantiateViewController(withIdentifier: "resultsView") as? GoalsVC else { return }
resultsVC.goalDelegate = self
self.addChild(resultsVC)
resultsVC.view.frame = subView.bounds
self.subView.addSubview(resultsVC.view)

posted this

Have an answer?

JD

Please login first before posting an answer.