【问题标题】:Resize child view controller without constraints (Auto Layout) in a container view在容器视图中调整子视图控制器的大小(自动布局)
【发布时间】: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;
}

【问题讨论】:

标签: ios objective-c uicontainerview


【解决方案1】:

它必须是targetVC.view.frame = self.containerView.bounds; 而不是targetVC.view.frame = self.containerView.frame;

【讨论】:

  • 您能多展示一些您的 UI 设置,或者是实际问题的屏幕截图吗?
  • 谢谢,它成功了。 (我在第一次尝试时给故事板中的容器视图提供了错误的高度值,在修复它之后,您的回答解决了我的问题。)
  • 完美解决方案!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2013-07-16
  • 2016-05-03
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多