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
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- URL编码Swift iOS 2022-01-01
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01