我在 VC 中有一个全屏的 containerView.如果我从 Storyboard 手动将子项添加到 containerView 中,则进行嵌入 segue 看起来很好:
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:
但如果我通过代码嵌入 VC:
But if I embed the VC by code:
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:
你需要告诉你的 BannerContainer 视图控制器它有一个新的子控制器,并告诉 Child 它有一个父 VC.Apple Docs 此处对此进行了描述.像这样:
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];
或者在 Swift 中:
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)
这样可以保证各种布局和触摸方法都传递给子VC;我怀疑您遇到的布局问题可能是由于当前未调用这些方法.
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.
这篇关于在容器视图中加载 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!