Ios 如何在不破坏复杂布局的情况下从容器视图中删除?

Ios 如何在不破坏复杂布局的情况下从容器视图中删除?,ios,objective-c,autolayout,uikit,uicontainerview,Ios,Objective C,Autolayout,Uikit,Uicontainerview,我有一个场景是建立在附加的屏幕上 根据从顶部栏中选择的按钮,我将在容器视图中加载特定的视图控制器。当更改选择或用户点击控制器上的“后退”按钮时,应删除容器的固有内容 添加到容器视图的代码: if (self.containerVC != nil) { [self removeController]; } UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; FirstViewC

我有一个场景是建立在附加的屏幕上

根据从顶部栏中选择的按钮,我将在容器视图中加载特定的视图控制器。当更改选择或用户点击控制器上的“后退”按钮时,应删除容器的固有内容

添加到容器视图的代码:

if (self.containerVC != nil) {
    [self removeController];
}

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FirstViewController* firstVC = [storyboard instantiateViewControllerWithIdentifier:@"First"];

[self addChildViewController:firstVC];
[self.containerVieww addSubview:firstVC.view];
firstVC.view.frame = CGRectMake(0, 0, self.containerVieww.bounds.size.width, self.containerVieww.bounds.size.height);
[firstVC.view layoutSubviews];
[firstVC didMoveToParentViewController:self];

firstVC.modalPresentationStyle = UIModalPresentationCurrentContext;

[UIView animateWithDuration:0.5 animations:^{
    self.logViewHeight.constant = self.contentView.frame.size.height * 0.9;
    [firstVC.view layoutSubviews];
}];

firstVC.delegate = self;
self.containerVC = firstVC;
[self.containerVC dismissViewControllerAnimated:YES completion:nil];

[UIView animateWithDuration:0.5 animations:^{
    self.logViewHeight.constant = 0;
    [self.view layoutIfNeeded];
 }];

[self willMoveToParentViewController:nil];
[self removeFromParentViewController];
[self.containerVC.view removeFromSuperview];    // Caused layout destoying !!!
self.containerVC = nil;
用于从容器视图中删除的代码:

if (self.containerVC != nil) {
    [self removeController];
}

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FirstViewController* firstVC = [storyboard instantiateViewControllerWithIdentifier:@"First"];

[self addChildViewController:firstVC];
[self.containerVieww addSubview:firstVC.view];
firstVC.view.frame = CGRectMake(0, 0, self.containerVieww.bounds.size.width, self.containerVieww.bounds.size.height);
[firstVC.view layoutSubviews];
[firstVC didMoveToParentViewController:self];

firstVC.modalPresentationStyle = UIModalPresentationCurrentContext;

[UIView animateWithDuration:0.5 animations:^{
    self.logViewHeight.constant = self.contentView.frame.size.height * 0.9;
    [firstVC.view layoutSubviews];
}];

firstVC.delegate = self;
self.containerVC = firstVC;
[self.containerVC dismissViewControllerAnimated:YES completion:nil];

[UIView animateWithDuration:0.5 animations:^{
    self.logViewHeight.constant = 0;
    [self.view layoutIfNeeded];
 }];

[self willMoveToParentViewController:nil];
[self removeFromParentViewController];
[self.containerVC.view removeFromSuperview];    // Caused layout destoying !!!
self.containerVC = nil;
调用“removeFromSuperview”后,我的布局中的“蓝色视图”消失


如何在不破坏复杂布局的情况下从容器视图中删除?

不太清楚您在做什么。。。您正在设置
firstVC.modalPresentationStyle
,然后在删除时调用
dismissViewControllerAnimated
,但您没有显示任何
presentViewController
?您是否在调试控制台中收到任何错误/警告消息?在
蓝色视图
消失后,您是否使用
调试视图层次结构
检查对象?基本上,您必须执行
[firstVC.View removeFromSuperview]。你要删除的不是你要添加的,而是谢谢你们的评论。上次我注意到在删除视图期间调用函数的顺序是错误的。我已经更改了它们,错误消失了。所以问题就解决了。