SwiftUI - Resizable List height that dependent on an element count

前端 未结 2 716
忘掉有多难
忘掉有多难 2021-01-18 14:12

I have some troubles with dynamically changing List height that dependent on elements count.

I tried this solution but it didn\'t work.

List {
    Fo         


        
2条回答
  •  星月不相逢
    2021-01-18 15:10

    Self size List:

    If you want a List to show it's content all at once, It means you don't need the recycling feature (the key feature of the list), So all you need is to not using a List! Instead, you can use ForEach directly, then it will size itself based on it's content:

    ForEach(searchService.searchResult, id: \.self) { item in
        VStack(alignment: .leading) {
            Text(item).font(.custom("Avenir Next Regular", size: 12))
            Divider()
        }.padding(.horizontal, 8)
    }
    

    You can change all sizes and spacings according to your needs

    Note that You can use LazyVStack from iOS 14 to make it lazy-load and boost its performance.

提交回复
热议问题