Search code examples
swiftswiftuiswiftui-list

How to reduce height of a View in a List in SwiftUI


Is it possible to reduce the height of a row in a SwiftUI List? I can't seem to get it to go smaller than the default height.

I tried adding .listRowInsets(EdgeInsets()) on ForEach but it just changes the trailing and leading padding.

In UIKit, I could just use the tableView(_:heightForRowAt:) to adjust the height to the desired size.

struct ContentView: View {
    var body: some View {
        List {
            ForEach(1...10, id: \.self) { i in
                Text("\(i)")
            }
        }
    }
}

enter image description here


Solution

  • Change environment property defaultMinListRowHeight like this:

            List {
                ForEach(1...10, id: \.self) { i in
                    Text("\(i)")
                }.frame(height: 2)
            }.environment(\.defaultMinListRowHeight, 1)
        
    

    enter image description here