Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 容器的约束问题_Ios_Nslayoutconstraint_Uicontainerview - Fatal编程技术网

Ios 容器的约束问题

Ios 容器的约束问题,ios,nslayoutconstraint,uicontainerview,Ios,Nslayoutconstraint,Uicontainerview,好的,我有一个视图控制器,它位于另一个视图的容器中 MainView View Someview ContainerView (contains ContainedViewController) Otherview ContainedViewController ContainedView (height can get resized during run). UILabel (varying height) UIView (fixe

好的,我有一个视图控制器,它位于另一个视图的容器中

MainView
  View
    Someview
    ContainerView (contains ContainedViewController)
    Otherview

ContainedViewController
   ContainedView (height can get resized during run).
      UILabel (varying height)
      UIView (fixed size, width & height)
事情是这样的。。。ContainedViewController中的视图需要在运行时调整大小。它包含一个标签,该标签可以根据其中的文本而增长,并在其正下方包含一个静态视图,该视图的大小永远不会改变

因此,我对UILabel的高度有一个约束,我在运行时根据需要的大小对其进行更改。在标签和固定视图之间有一个垂直约束,以及主superview的所有标准约束

但是,当我运行时,我无法同时满足约束,然后是约束日志。冲突的约束包括我的新UILabel高度,然后是ContainedView的高度。我在ContainedView中遇到了UIView封装视图高度问题

显然,ContainedView的高度是由MainView的ContainerView的高度决定的

我想让ContainedView的高度在UILabel的高度改变时改变,然后让propagate备份容器,一直到MainView。但我似乎没法让它发挥作用


如何获取superview以及更改标签大小时要调整其容器视图大小?

在主视图控制器中,声明以下覆盖:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    // Layout the container's content and get its resulting size
    [self.containedViewController.view layoutIfNeeded];
    CGSize containedViewSize = [self.containedViewController contentSize];
    // Now, use containedViewSize to set constraints on the view controller.
    self.containerHeightConstraint.constant = containedViewSize.height;
    self.containerWidthConstraint.constant = containedViewHeight.width;
}
然后,在包含的视图控制器中声明以下属性:

@property (nonatomic, weak) IBOutlet UIView * contentView;
@property (nonatomic, weak) UIViewController * mainViewController;
在故事板中,使contentView成为包含的视图控制器主视图的子视图。使用约束将其固定到主视图的左上角。在contentView中放入您想要的任何内容,包括决定contentView大小的约束。但是,不要将contentView的长度和宽度固定到主视图;这将导致上述约束冲突

在准备设置容器视图控制器的嵌入序列时,应设置mainViewController

这种方法:

- (CGSize)contentSize {
    return self.contentView.frame.size;
}
现在,每当您在容器视图控制器中执行影响contentView大小的操作时,请执行以下调用:

[self.mainViewController.view setNeedsLayout];
很抱歉,这并不简单,但这是迄今为止我找到的解决这个问题的最佳方案