嵌入UIViewController的子容器的自动布局

我的主场景显示了一个子UIViewController ,通过在其View中放置一个Child Container,并在该子容器中嵌入一个子UIViewController。 我通过从子容器中UIViewController Ctrl键拖动到storyboard的子UIViewController ,然后选择“嵌入”segue。

这让我可以在其他地方封装和重用子UIViewController 。 我认为这是相当标准的。 但我无法正确地进行自动布局。

我做了一个简单的测试用例来certificate这一点: https : //github.com/murraycu/ios-example-autolayout-of-child-container-views

它有两个嵌入Tab控制器的UIViewControllers ,因此您可以在它们之间切换。

“简单”选项卡显示SimpleViewController ,它显示图像和标签,在故事板中可以看到:

SimpleViewController

UIImage或UILabel都没有指定高度约束,尽管它们具有等宽(等于父视图)约束以保持宽度简单。

UILabel显然不是很高,但是UIImage和UILabel的内容拥抱优先级略有不同,根据Auto Layout的说法,它们的高度并不含糊。 因此,由于自动布局,当我在运行时将其文本设置为需要更多空间的文本时,它会占用更多空间,从而远离UIImage上方的空间。 这很好 – 这是我想要的行为,如模拟器中所示:

模拟器中的SimpleViewController

现在问题:“With Child Container”选项卡显示WithChildContainerViewController,它显示相同的图像,但显示我的ChildViewController(嵌入在子容器中)而不是UILabel。 嵌入式ChildViewController显示UILabel。 这是在故事板中:

WithChildContainerViewController

但是,自动布局系统现在似乎不知道子容器需要多少空间来显示我的ChildViewController中标签中的所有文本。 与“简单”选项卡中一样,UIImage或子容器都没有指定高度约束。 现在XCode抱怨“高度对于容器视图是不明确的”。它在模拟器中看起来像这样:

模拟器中的WithChildContainerViewController

如果我向子容器添加一个约束,将其底部约束到父视图的底部,这会得到改善,如@iphonic所建议的那样: https : //github.com/murraycu/ios-example-autolayout-of-child-container -views / commit / 1d295fe0a6c4502764f8725d3b99adf8dab6b9ae但高度仍然是错误的:

在此处输入图像描述

如何让汽车布局系统知道该怎么做? 我已经考虑过实现UIView的intrinsicContentSize,但是UIViewController不是UIView。

建议是,以编程方式而不是通过IB进行。 见下文

  _childController=[self.storyboard instantiateViewControllerWithIdentifier:@"ChildController"]; [self addChildViewController:_childController]; [_container addSubview:_childController.view]; [_childController didMoveToParentViewController:self]; _childController.view.frame=_container.bounds; //add constraints UIView *subView=_childController.view; UIView *parent=_container; subView.translatesAutoresizingMaskIntoConstraints=NO; NSLayoutConstraint *width =[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:parent attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]; NSLayoutConstraint *height =[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:parent attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]; NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parent attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f]; NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:parent attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f]; [parent addConstraint:width]; [parent addConstraint:height]; [parent addConstraint:top]; [parent addConstraint:leading]; 

希望能帮助到你。

干杯。

@ iphonic的代码,但使用视觉格式语言

 _childController = [self.storyboard instantiateViewControllerWithIdentifier: @"ChildController"]; [self addChildViewController: _childController]; [_container addSubview: _childController.view]; [_childController didMoveToParentViewController: self]; //add constraints NSDictionary *views = @{@"subView": _childController.view, @"parent": _container}; [parent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|[subView(==parent)]" options: 0 metrics: nil views: views]]; [parent addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView(==parent)]" options: 0 metrics: nil views: views]];