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

mur*_*ayc 5 uiviewcontroller uilabel ios ios-autolayout

我的主要场景UIViewController通过在其视图中放置一个子容器,并在该子容器中嵌入子UIViewController来显示一个孩子.我这样做,从子容器CTRL拖动到孩子UIViewControllerstoryboard,并选择"嵌入" SEGUE.

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

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

它有两个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.

iph*_*nic 3

建议以编程方式而不是通过 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];
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。

干杯。