Berry Blue
Berry Blue

Reputation: 16482

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

Upvotes: 1

Views: 734

Answers (1)

AdR
AdR

Reputation: 882

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

Upvotes: 2

Related Questions