列表中的额外间距 iOS 14 SwiftUI

Extra Spacing in List iOS 14 SwiftUI

我正在尝试从 ListView 中删除外部 space(标记为红色)。

此问题出现在 iOS 14 和 Xcode 12 GM 中。

这是我使用的代码:

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)
    }
}

要使 .listRowInsets 正常工作,您需要在 List:

中使用 ForEach
List {
    ForEach(self.menuItems, id:\.self) {
        MenuRow(title: [=10=])
    }
}

这对我有用。对于列表添加:

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