Search code examples
iosswiftlistswiftuiios14

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


Solution

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

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