简体   繁体   English

如何根据子视图的高度设置堆栈视图的高度?

[英]How to set stack view height according to its subviews height?

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 (ie 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. 问题是:我无法设置大约UIView的固定高度,并且不会根据标签文本调整大小。 Please suggest the appropriate solutions. 请提出适当的解决方案。

Hi you can get the intrinsic height of the stackView like this: 嗨,您可以像这样获取stackView的固有高度:

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 :) 以为任何人寻找这个可能有用:)

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. 我要指出的第一件事是UIStackView可以通过其排列的子视图的大小来推断其大小。 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. 也许您没有做出合理的约束。

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的轴是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 is UILayoutConstraintAxisVertical . UIStackView的轴为UILayoutConstraintAxisVertical
  • And your UIStackView (or all of your content) has a fixed width. 而且您的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. 因为UILabel不必具有固定的大小。 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. 您只需要指定其宽度(或指定其前导和尾随其超级视图),它的高度将根据其字体和内容自动计算。

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. 以及标题标签的顶部间隙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM