SwiftUI 中表单部分的清晰背景?

Clear background for form sections in SwiftUI?

我正在尝试删除某些部分的白色背景,以便元素正好位于灰色背景上,但我无法删除或透明部分背景。

这就是我正在尝试的:

struct ContentView: View {
    
    var body: some View {
        Form {
            Section {
                Text("Hello!")
                Button {
                    print("Clicked")
                } label: {
                    Text("Click Me!!")
                }
            }
            Section {
                VStack {
                    Button("Button 1") {}
                    Spacer()
                    Button("Button 2") {}
                }
            }
            .background(Color.clear) // Not doing anything
            Section {
                VStack(alignment: .leading) {
                    Text("Location")
                        .font(.headline)
                    Group {
                        Text("Abc")
                        Text("Abc")
                        Text("Abc")
                    }
                    .font(.caption)
                }
            }
        }
    }
}

这是它的样子:

我尝试将.background(Color.clear)添加到SectionVStack中,但没有任何效果。如何在 SwiftUI 中实现这一点?

即使在 SwiftUI 2 中,Form 也是建立在 UIKit 之上的,特别是 UITableView

您需要删除默认 UITableViewCell 的背景(仅一次,最好在 App init 中):

UITableViewCell.appearance().backgroundColor = UIColor.clear

并使用以下方法更改背景:

Section {
    VStack {
        Button("Button 1") {}
        Spacer()
        Button("Button 2") {}
    }
}
.listRowBackground(Color.clear)