ios 在容器视图中加载 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28852338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:06:19  来源:igfitidea点击:

Loading a ViewController inside a Container View

iosswiftstoryboardautolayout

提问by Godfather

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: enter image description here

我在 VC 中有一个全屏的 containerView。如果我从 Storyboard 手动将一个孩子添加到 containerView 做一个嵌入 segue 看起来很好: 在此处输入图片说明

But if I embed the VC by code:

但是如果我通过代码嵌入 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)
    }
}

I get super strange results:

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

enter image description here

在此处输入图片说明

回答by pbasdf

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:

你需要告诉你的 BannerContainer 视图控制器它有一个新的子控制器,并告诉子它有一个父 VC。这在此处的 Apple 文档中有所描述。像这样:

   [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:

或者在 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.

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

回答by Luiz Dias

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

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

Updated for Swift 3:

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

回答by Sarath Kumar Rajendran

Update for Swift 4.2/5.

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.

ListViewController 是另一个要嵌入的视图控制器。

回答by DEEPAK KUMAR

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

回答by Dmih

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

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

这里采用

回答by RiceAndBytes

Updated for Swift 4

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