Paweł Pela
Paweł Pela

Reputation: 451

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?

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. 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.

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.

Upvotes: 6

Views: 2995

Answers (1)

LuLuGaGa
LuLuGaGa

Reputation: 14388

You should use a VStack instead of a 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.

Upvotes: 2

Related Questions