在SwiftUI中为表单节清除背景吗?

时间:2020-09-17 20:48:22

标签: 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)
                }
            }
        }
    }
}

这是它的样子:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

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

UITableViewCell.appearance().backgroundColor = UIColor.clear

,然后使用以下方法更改背景:

Section {
    VStack {
        Button("Button 1") {}
        Spacer()
        Button("Button 2") {}
    }
}
.listRowBackground(Color.clear)
相关问题