我有一个.xib文件,我想添加一个容器视图(放置在ViewController内).不幸的是,容器视图只能由故事板一次性使用.但是当我创建一个.xib文件,我搜索了容器视图控制器,我没有找到它.有人可以给我一个提示如何实现我的任务吗?
解决方法
如果您使用的是xib而不是故事板,则可以将简单的UIView添加到xib作为容器.然后在代码中,将您的childViewController的视图添加为容器的子视图.在这里,我已经遵循适当的子视图控制器方法和添加的布局约束,以确保其框架更新与容器的框架:
- - (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;
- }