如何根据子视图的与自动布局layouted大小调整父视图

问题描述:

我是preparing这类似于截图所示的例子中自定义视图。我在运行时添加的所有用户界面组件(标签,文本字段,单选按钮,..)。要定位我用Autolayout约束。这部分工作正常为止。

I am preparing a custom view which looks similar to the example shown in the screenshot. I add all user interface components (labels, text fields, radio buttons, ..) at runtime. To position the elements I use Autolayout constraints. This part works fine so far.

元素的数量的语境而变化。我不知道如何使用自动布局约束的动态调整父视图(最有可能的视图的高度)。因此,鉴于限制应考虑当前连接的子视图定义的高度和空间。

The number of elements varies from context to context. I wonder how I could use Autolayout constraints to dynamically resize the parent view (most likely the height of the view). The view constraints therefore should consider the height and margins defined for the currently attached subviews.

我用下面的设置做到了这一点:

I've done this with the following setup:


  • 容器视图没有高度约束

  • 顶子视图具有从容器视图的顶部
  • 间隔约束
  • 的意见包含的所有有他们之间的间距限制

  • 底部视图有间距约束到容器视图底部

自动布局系统通过调整容器适合其内容满足这些约束。

The auto layout system satisfies these constraints by resizing the container to fit its contents.

当然,你需要为你改变的内容动态地设置的限制。

Of course, you need to dynamically set the constraints as you alter the contents.