简体   繁体   English

如何在不滚动 SwiftUI 的情况下显示整个列表

[英]How to show the entire List without scrolling in SwiftUI

Is there a way to make a List be of height that matches its content automatically?有没有办法让 List 的高度自动匹配其内容?

I have a list that will show a number of items (I don't know in advance how many).我有一个列表会显示一些项目(我事先不知道有多少)。 By default, when I create the list, it doesn't get enough height, instead it shows 1-3 items and scrolls to show more.默认情况下,当我创建列表时,它没有得到足够的高度,而是显示 1-3 个项目并滚动显示更多。 I would like to disable the scrolling behaviour.我想禁用滚动行为。

I tried to hardcode frame height to some large value that I'm certain will fit all the items, but this isn't ideal.我尝试将框架高度硬编码为某个我确定适合所有项目的较大值,但这并不理想。 I can also calculate the frame height from itemHeight * numItems (if I set all the items to specified height themselves), but in my case the items can have varying heights.我还可以从 itemHeight * numItems 计算框架高度(如果我将所有项目自己设置为指定的高度),但在我的情况下,这些项目可以有不同的高度。

Maybe there's a simpler solution for that?也许有一个更简单的解决方案?

I looked in the documentation, tried also.fixedSize(), different list styles etc. Those didn't work for me.我查看了文档,也尝试了.fixedSize(),不同的列表 styles 等。这些对我不起作用。

You should use a VStack instead of a List .您应该使用VStack而不是List This will get rid of the scrolling behavior, but will still give vertically organized cells.这将摆脱滚动行为,但仍会提供垂直组织的单元格。 You can still use ForEach inside if that's what you are doing inside List .如果您在List中这样做,您仍然可以在其中使用ForEach

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

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