我的主场景显示了一个子 UIViewController
,方法是在其 View 中放置一个子容器,然后在该子容器中嵌入一个子 UIViewController。为此,我按住 Ctrl 从子容器拖动到 storyboard
中的子 UIViewController
,然后选择“嵌入”segue。
这让我可以在别处封装和重用子 UIViewController
。我认为这是相当标准的。但是我不能让它正确地自动布局。
我做了一个简单的测试用例来证明这一点: https://github.com/murraycu/ios-example-autolayout-of-child-container-views
它有两个嵌入在选项卡 Controller 中的 UIViewControllers
,因此您可以在它们之间切换。
“简单”选项卡显示 SimpleViewController
,它显示图像和标签,在 Storyboard中可以看到:
UIImage 或 UILabel 均未指定高度约束,但它们具有等宽(等于父 View )约束以保持宽度简单。
UILabel 显然不是很高,但 UIImage 和 UILabel 的 Content Hugging 优先级略有不同,因此根据自动布局,它们的高度不会模糊。因此,多亏了自动布局,当我在运行时将其文本设置为一些需要更多空间的文本时,它会占用更多空间,从而占用其上方 UIImage 的空间。这很好 - 这是我想要的行为,如在模拟器中所见:
现在问题来了:“With Child Container”选项卡显示 WithChildContainerViewController,它显示相同的图像但显示我的 ChildViewController(嵌入在子容器中)而不是 UILabel。嵌入的 ChildViewController 显示 UILabel。这是在 Storyboard 中:
但是,自动布局系统现在似乎不知道子容器需要多少空间来显示我的 ChildViewController 标签中的所有文本。与“简单”选项卡中一样,UIImage 或子容器都没有指定高度约束。现在 XCode 提示“容器 View 的高度不明确”。它在模拟器中看起来像这样:
如果我向子容器添加约束,将其底部限制在父 View 的底部,这会得到改善,正如@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];
希望对您有所帮助。
干杯。
关于ios - 嵌入 UIViewController 的子容器的自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998946/