我想使用带有两个 subview Controller 的容器 View 。但问题是,当我用下面的代码更新 subview Controller 框架时, subview Controller 的下面部分是不可见的。
@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
有些解决方案带有约束,但我的项目 Storyboard 不使用自动布局(约束)。
对我的问题的解决方案有什么意见吗?
(也许我必须找到另一种不使用容器 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;
.
关于ios - 在容器 View 中无约束(自动布局)调整 subview Controller 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51058530/