Loading a ViewController inside a Container View

后端 未结 6 1716
夕颜
夕颜 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: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.

提交回复
热议问题