在容器视图中无约束(自动布局)调整子视图控制器的大小
Resize child view controller without constraints (Auto Layout) in a container view
我想使用带有两个子视图控制器的容器视图。但问题是,当我用下面的代码更新子视图控制器框架时,子视图控制器的下面部分是不可见的。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
}
- (void)removeViewController
{
[currentVC.view removeFromSuperview];
[currentVC removeFromParentViewController];
}
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
targetVC.view.frame = self.containerView.frame;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
}
- (IBAction)firstOpen:(id)sender
{
[self bindToViewController:firstVC];
}
- (IBAction)secondOpen:(id)sender
{
[self bindToViewController:secondVC];
}
@end
有些解决方案带有约束,但我的项目情节提要不使用自动布局(约束)。
对我的问题的解决方案有什么意见吗?
(也许我得另辟蹊径,不用container view)
****** 编辑 1 ******
我在 bindToViewController 的末尾添加了 didMoveToParentViewController 但没有改变。
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
targetVC.view.frame = self.containerView.frame;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
[targetVC didMoveToParentViewController:self];
}
****** 编辑 2 - 解决方案 ******
尝试了 André Slotta 的建议,成功了!
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
// targetVC.view.frame = self.containerView.frame;
targetVC.view.frame = self.containerView.bounds;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
}
它必须是 targetVC.view.frame = self.containerView.bounds;
而不是 targetVC.view.frame = self.containerView.frame;
。
我想使用带有两个子视图控制器的容器视图。但问题是,当我用下面的代码更新子视图控制器框架时,子视图控制器的下面部分是不可见的。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
}
- (void)removeViewController
{
[currentVC.view removeFromSuperview];
[currentVC removeFromParentViewController];
}
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
targetVC.view.frame = self.containerView.frame;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
}
- (IBAction)firstOpen:(id)sender
{
[self bindToViewController:firstVC];
}
- (IBAction)secondOpen:(id)sender
{
[self bindToViewController:secondVC];
}
@end
有些解决方案带有约束,但我的项目情节提要不使用自动布局(约束)。
对我的问题的解决方案有什么意见吗?
(也许我得另辟蹊径,不用container view)
****** 编辑 1 ******
我在 bindToViewController 的末尾添加了 didMoveToParentViewController 但没有改变。
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
targetVC.view.frame = self.containerView.frame;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
[targetVC didMoveToParentViewController:self];
}
****** 编辑 2 - 解决方案 ******
尝试了 André Slotta 的建议,成功了!
- (void)bindToViewController:(UIViewController *)targetVC
{
if (currentVC != nil)
{
[self removeViewController];
}
[self addChildViewController:targetVC];
// targetVC.view.frame = self.containerView.frame;
targetVC.view.frame = self.containerView.bounds;
[self.containerView addSubview:targetVC.view];
currentVC = targetVC;
}
它必须是 targetVC.view.frame = self.containerView.bounds;
而不是 targetVC.view.frame = self.containerView.frame;
。