在容器视图中加载 ViewController

Loading a ViewController inside a Container View

我在 VC 中有一个全屏的 containerView。如果我从情节提要中手动将 child 添加到 containerView 中,那么嵌入 segue 看起来不错:

但是如果我通过代码嵌入 VC:

class BannerContainerVC: UIViewController {

    @IBOutlet weak var container: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = storyboard?.instantiateViewControllerWithIdentifier("test") as UIViewController
        self.container.addSubview(vc.view)
    }
}

我得到了超级奇怪的结果:

您需要告诉您的 BannerContainer 视图控制器它有一个新的 child 控制器,并告诉 Child 它有一个 parent VC。这在 Apple 文档 here 中有描述。像这样:

   [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];

或在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)

这确保各种布局和触摸方法都传递给 child VC;我怀疑您遇到的布局问题可能是由于当前未调用这些方法造成的。

尝试使用上面的答案,但结果 CGRectMake 不再可用。

更新 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)

更新 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)

只需调用这个函数:-

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)
 }

更新 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 是另一个要嵌入的视图控制器。

Swift 5.0 使用此函数添加和删除 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()
    }

采纳自here