在从.xib创建的UIViewController内添加ContainerView [英] Add a ContainerView inside a UIViewController created from .xib
问题描述
我有一个.xib文件,我想向它添加一个容器视图(放置在ViewController中).不幸的是,容器视图只能由情节提要一次性使用.但是,当我创建一个.xib文件并搜索容器视图控制器时,没有找到它.有人可以给我提示如何完成我的任务吗?
I have a .xib file and i want to add it a container view (to place inside a ViewController). Unfortunately a container view is only disposable by storyboard. But when i create a .xib file and i search for the container view controller, i don´t found it. Can someone give me a tips how to achieve my task?
推荐答案
如果您使用的是xib
而不是storyboard
,则只需在xib
上添加普通的UIView
即可一个容器.然后在代码中,将childViewController的view
添加为容器的子视图.在这里,我遵循了适当的子视图控制器方法,并添加了布局约束,以确保其框架随着容器的框架而更新:
If you're using a xib
instead of a storyboard
, you can just add a plain UIView
to the xib
to act as a container. Then in code, add your childViewController's view
as a subview of the container. Here, I've followed the appropriate child view controller methods and added layout constraints to ensure its frame updates with the container's frame:
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *childViewController = ...; // create your child view controller
[self addChildViewController:childViewController];
[self.containerView addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
NSArray *horzConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[childView]|"
options:0
metrics:nil
views:@{@"childView" : childViewController.view}];
NSArray *vertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[childView]|"
options:0
metrics:nil
views:@{@"childView" : childViewController.view}];
[self.view addConstraints:horzConstraints];
[self.view addConstraints:vertConstraints];
childViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
}
这篇关于在从.xib创建的UIViewController内添加ContainerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!