【发布时间】:2018-06-27 08:55:39
【问题描述】:
我想将容器视图与两个子视图控制器一起使用。但问题是当我用下面的代码更新子视图控制器框架时,子视图控制器的下面部分不可见。
@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
有一些带有约束的解决方案,但我的项目故事板不使用自动布局(约束)。
对我的问题的解决方案有什么意见吗?
(也许我必须在不使用容器视图的情况下找到另一种设计方式)
****** 编辑 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;
}
【问题讨论】:
-
我怀疑它会解决你的问题,但你也应该在
bindToViewController:方法的末尾调用[targetVC didMoveToParentViewController:self]。相关文档:developer.apple.com/documentation/uikit/uiviewcontroller/… -
我用你的提议编辑了我的问题,但没有改变。
标签: ios objective-c uicontainerview