UIStackView内部的UIView动态高度 [英] UIView dynamic height inside UIStackView

查看:1517
本文介绍了UIStackView内部的UIView动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个堆栈视图设置,其中包含许多不同的对象,我正在尝试向其中添加视图.

I currently have a Stack View setup with a bunch of different objects inside of it, and I'm trying to add a view to it.

该视图将在运行时以编程方式在其中添加动态数量的标签.

The view will have a dynamic number of labels added inside of it programmatically at run-time.

将视图放入堆栈视图时,会遇到约束错误,这些错误可以通过添加高度约束来解决.

When I put the view inside of the stack view, I get errors with the constraints, which get fixed by adding a height constraint.

但是,我最初不知道视图的高度,所以我不能限制它.

However, I don't know the height of the view initially so I can't constrain this.

有什么方法可以根据其内容大小在StackView自动调整大小内包含UIView吗?我以为这是自动的,但是当我不设置高度限制时,视图不会显示

Is there any way to have a UIView inside of a StackView auto-resize based on it's content size? I thought this would be automatic, but when I don't put a height constraint, the view doesn't show up

推荐答案

您不必将内容视图放入stackview内即可向其中添加元素,您只需将stackview与(前导,尾随,顶部)对superView的约束

You don't have to put a content view inside the stackview to add the elements to it , you need to hook the stackview with only ( leading , trailing , top) constraints to the superView

然后使用

stack.addArrangedSubview(view)

最重要的是,您添加的元素必须具有内在的内容大小,例如label和button,否则,请添加高度

the most important thing is that the elements you add must have an intrinstic content-size like label and button , and if not then add a height with

someView.heightAnchor.constraint(equalToConstant:<#setValue#>).isActive = true

将分配设置为填充"

这篇关于UIStackView内部的UIView动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆