Loading a ViewController inside a Container View(在容器视图中加载 ViewController)
问题描述
我在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在容器视图中加载 ViewController
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01