ios - 在容器 View 中无约束(自动布局)调整 subview Controller 的大小

标签 ios objective-c uicontainerview

我想使用带有两个 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/

相关文章:

ios - swift : One Navigation bar item

ios - 如何在 iOS 视频播放器中播放 webm 文件

ios - 如何在方法调用后重定向谷歌页面

ios - swift 3 从父 ViewController 调用函数

objective-c - 选择行时 UIPickerView 不会被删除

iphone - 在这种情况下,可以一次又一次地重新分配窗口的 rootviewcontroller 吗?

ios - 使用 Autolayout 和/或 preferredFontForTextStyle 时,UITextview 被随机剪裁

ios - openFileOutput() 、 opernFileInput() 在 iOS 中等效(将 Android 应用程序移植到 iOS 平台)

ios - 来自可重复使用的表格单元格的不同 segues

ios - UIView 可以自动获取 UIViewController 的引用吗?