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

Tru*_*an1 11 ios swiftui ios14

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

这就是我正在尝试的:

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)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它看起来是这样的:

在此输入图像描述

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

paw*_*222 16

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

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

UITableViewCell.appearance().backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)

并使用以下命令更改背景:

Section {
    VStack {
        Button("Button 1") {}
        Spacer()
        Button("Button 2") {}
    }
}
.listRowBackground(Color.clear)
Run Code Online (Sandbox Code Playgroud)

  • 我认为这会改变全球所有表格单元格的值?但仍然感觉很hacky。Color.clear 确实应该单独工作。相反,这似乎非常接近:`.listRowBackground(Color(.systemGroupedBackground))` (9认同)