xcode 如何根据其子视图高度设置堆栈视图高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48182001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set stack view height according to its subviews height?
提问by Andro Developer
I have a requirement to scroll six different type of view, Each view has different size, One of the views is: About Me. In About Me, I have to show the title (i.e. ABOUT ME) and text below it and label height should be according to text height.
我需要滚动六种不同类型的视图,每个视图都有不同的大小,其中一个视图是:关于我。在关于我中,我必须显示标题(即关于我)和其下方的文本,标签高度应根据文本高度。
Till Now I have designed this requirement as:
到目前为止,我已将此要求设计为:
Root View - Stack View * Item 1 with fix height * Item 2 with fix height * UIView as About Me: - Label as title - Label as text with max lines set to 0 and width equals to container width.
根视图 - 堆栈视图 * 项目 1 具有固定高度 * 项目 2 具有固定高度 * UIView 作为关于我: - 标签为标题 - 标签为文本,最大行数设置为 0,宽度等于容器宽度。
The problem is: I can not set fix height of about me UIView and it does not resize according to label text. Please suggest the appropriate solutions.
问题是:我无法设置关于我的 UIView 的固定高度,并且它不会根据标签文本调整大小。请提出适当的解决方案。
回答by NuN
Hi you can get the intrinsic height of the stackView like this:
嗨,您可以像这样获得 stackView 的内在高度:
let size = stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
let size = stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
I know Im kinda late to reply to this message. But I had the same issue and I found this piece of code. Thought anyone who looking for this might be useful :)
我知道我回复这个消息有点晚了。但是我遇到了同样的问题,我找到了这段代码。认为任何寻找这个的人都可能有用:)
回答by Turtleeeeee
I don't think that you declare your problem clearly. The first thing I want to point out is that UIStackView
can infer its size by the size of its arranged subview. But you must first confirm that the constraints added to the subviews are proper. So if you don't get the resizing effect you want, you must be missing something. Maybe you're not making the reasonable constraints.
我认为你没有清楚地说明你的问题。我想指出的第一件事是,UIStackView
可以通过其排列的子视图的大小来推断其大小。但是你必须首先确认添加到子视图的约束是正确的。所以如果你没有得到你想要的调整大小的效果,你一定是遗漏了一些东西。也许你没有做出合理的限制。
To clarify your situation, you'd better add some screenshot of your constraints added to your 'About Me' view. By the way, what's the axis of your UIStackView
?
为了澄清您的情况,您最好在“关于我”视图中添加一些约束的屏幕截图。顺便问一下,你的轴是UIStackView
什么?
I don't know either of these. So I have to make some assumption for me to explain more about your problem.
这两个我都不知道。所以我必须做一些假设来解释更多关于你的问题。
- The axis of your
UIStackView
isUILayoutConstraintAxisVertical
. - And your
UIStackView
(or all of your content) has a fixed width.
- 你的轴
UIStackView
是UILayoutConstraintAxisVertical
. - 并且您的
UIStackView
(或您的所有内容)具有固定的宽度。
With the above conditions, Defining a view that its height is decided by its text will be simple. Because UILabel
doesn't have to have a fixed size. You only need to specify its width(or specify its leading and trailing to its superview), and its height will be automatically computed based on its font and content.
有了上述条件,定义一个高度由其文本决定的视图将很简单。因为UILabel
不必有固定的大小。你只需要指定它的宽度(或指定它的前导和尾随到它的superview),它的高度将根据它的字体和内容自动计算。
Your About Me view has two label, so you should
Make sure of these things:
您的“关于我”视图有两个标签,因此您应该
确保以下事项:
- Title label: The top, leading, trailing constraints to the super view. and the bottom gap to the subtitle label(whatever you call).
- Subtitle label: The leading, trailing, bottom, constraints to the super view. and the top gap to the title label.
- 标题标签:超级视图的顶部、前导、尾随约束。和字幕标签的底部间隙(无论你叫什么)。
- 字幕标签:超级视图的前导、尾随、底部、约束。和标题标签的顶部间隙。