本文介绍了如何根据使用自动布局布局的子视图的大小调整父视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备一个自定义视图,它看起来类似于屏幕截图中显示的示例。我在运行时添加所有用户界面组件(标签,文本字段,单选按钮,..)。要定位元素,请使用约束。

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.

元素的数量因上下文而异。我不知道如何使用Autolayout约束来动态调整父视图(最有可能是视图的高度)。因此,视图约束应考虑为当前附加的子视图定义的高度和边距。

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.

推荐答案

setup:


  • 容器视图没有高度约束

  • 顶部子视图的间距限制

  • 所包含的视图之间都有空格限制

  • 底视图对容器底部有一个空格约束查看

  • the container view has no height constraint
  • the top subview has a spacing constraint from the top of the container view
  • the contained views all have spacing constraints between them
  • the bottom view has a spacing constraint to the bottom of the container view

自动布局系统通过调整容器大小以适应其内容来满足这些限制。

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.

这篇关于如何根据使用自动布局布局的子视图的大小调整父视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:56