Loading a ViewController inside a Container View

后端 未结 6 1605
夕颜
夕颜 2020-12-01 02:04

I have a containerView with full screen inside a VC. If i add a child to the containerView manually from a Storyboard doing a embed segue looks fine:

相关标签:
6条回答
  • 2020-12-01 02:42

    Swift 5.0 Use this functions to add and remove child VC:

    private func add(asChildViewController viewController: UIViewController, childFrame:CGRect) {
            // Add Child View Controller
            addChild(viewController)
    
            // Add Child View as Subview
            view.addSubview(viewController.view)
    
            // Configure Child View
            viewController.view.frame = childFrame
            viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
            // Notify Child View Controller
            viewController.didMove(toParent: self)
    
        }
        private func remove(asChildViewController viewController: UIViewController) {
            // Notify Child View Controller
            viewController.willMove(toParent: nil)
    
            // Remove Child View From Superview
            viewController.view.removeFromSuperview()
    
            // Notify Child View Controller
            viewController.removeFromParent()
        }
    

    adopted from here

    0 讨论(0)
  • 2020-12-01 02:44

    You need to tell your BannerContainer view controller that it has a new child controller, and to tell the Child that it has a parent VC. This is described in the Apple Docs here. Like this:

       [self addChildViewController:vc];
       vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
       [self.container addSubview:vc.view];
       [vc didMoveToParentViewController:self];
    

    Or in Swift:

        self.addChildViewController(vc)
        vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
        self.container.addSubview(vc.view)
        vc.didMoveToParentViewController(self)
    

    This ensures that various layout and touch methods are passed through to the child VC; I suspect the layout problems you have may be due to those methods not currently being called.

    0 讨论(0)
  • 2020-12-01 02:45

    Updated for Swift 4

    self.addChildViewController(vc)
    vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
    self.container.addSubview(vc.view)
    vc.didMovestrong text(self)
    
    0 讨论(0)
  • 2020-12-01 02:46

    Update for Swift 4.2/5.

    let listVc = ListViewController() 
    listVc.view.frame = CGRect(x: 0, y: 0, width: self.listView.frame.width, height: self.listView.frame.height)
    addChild(listVc)
    listVc.didMove(toParent: self)
    

    ListViewController is another view controller to be embedded.

    0 讨论(0)
  • 2020-12-01 03:02

    Just call this function: -

    private func addChildView(viewController: UIViewController, in view: UIView) {
        viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        viewController.view.frame = view.bounds
        addChild(viewController)
        view.addSubview(viewController.view)
        viewController.didMove(toParent: self)
     }
    
    0 讨论(0)
  • 2020-12-01 03:03

    Tried to use the answer above but it turns out CGRectMake isn't available anymore.

    Updated for Swift 3:

    self.addChildViewController(vc)
    vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
    self.container.addSubview(vc.view)
    vc.didMoveToParentViewController(self)
    
    0 讨论(0)
提交回复
热议问题