Malav Soni
Malav Soni

Reputation: 2838

Extra Spacing in List iOS 14 SwiftUI

I am trying to remove the outer space (marked in red) from ListView.

This issue appeared in iOS 14 and Xcode 12 GM.

Here is the code I am using:

struct ContentView: View {
    
    var menuItems:[String] = ["Item 1", "Item 2", "Item 3"]
    
    var body: some View {
        List(self.menuItems, id:\.self) { title in
            MenuRow(title: title)
        }
    }
}

struct MenuRow:View {
    var title:String
    var body: some View {
        HStack(alignment: .center) {
            Text(title)
                    .foregroundColor(Color.black)
                    .font(Font.system(size: 14,
                            weight: .regular,
                            design: .default))
            Spacer()
            Text(title)
                    .foregroundColor(Color.black)
                    .font(Font.system(size: 14,
                            weight: .regular,
                            design: .default))
            
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
        .listRowInsets(EdgeInsets())
        .background(Color.white)
    }
}

enter image description here

Upvotes: 1

Views: 1289

Answers (2)

Peter F.
Peter F.

Reputation: 246

This worked for me. For the list add:

List {
    ...
}
.environment(\.defaultMinListRowHeight, 23)

Upvotes: 0

pawello2222
pawello2222

Reputation: 54426

For .listRowInsets to work you need to use a ForEach inside a List:

List {
    ForEach(self.menuItems, id:\.self) {
        MenuRow(title: $0)
    }
}

Upvotes: 1

Related Questions