如何从 SwiftUI 部分中删除背景颜色?

Kyl*_*yle 3 swiftui

我有一个 SwiftUI 部分,如下所示:

struct FormView: View {
    var body: some View {
        Form {
            Section {
                Button(action: {
                    
                }) {
                    HStack {
                        Spacer()
                        Text("Save")
                            .font(.headline)
                            .foregroundColor(.white)
                        Spacer()
                    }
                }
                .listRowInsets(.init())
                .frame(height: 50)
                .background(Color.blue)
                .cornerRadius(15)
            }.background(Color.clear)
        }
        //.background(Color.red)
        .background(LinearGradient(gradient: Gradient(colors: [.green, .red]), startPoint: .leading, endPoint: .trailing))
        .edgesIgnoringSafeArea(.all)
    }
}

struct WelcomeScreen: View {
    var body: some View {
        FormView().background(Color.red)
            .onAppear {
               UITableView.appearance().backgroundColor = .clear
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

按钮

如何去掉该部分的白色背景颜色?我尝试添加 .background(Color.clear) 但没有任何作用。

Asp*_*eri 7

这是可能的解决方案 - 使用与父背景相同的列表行背景颜色。

使用 Xcode 12.1 / iOS 14.1 进行测试

演示

struct FormView: View {
    var body: some View {
        Form {
            Section {
                Button(action: {
                    
                }) {
                    HStack {
                        Spacer()
                        Text("Save")
                            .font(.headline)
                            .foregroundColor(.white)
                        Spacer()
                    }
                }
                .listRowInsets(.init())
                .listRowBackground(Color.red)     // << here !!
                .frame(height: 50)
                .background(Color.blue)
                .cornerRadius(15)
            }
        }.background(Color.red).edgesIgnoringSafeArea(.all)
    }
}
Run Code Online (Sandbox Code Playgroud)

处理非纯色时,添加

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

并设置

.listRowBackground(Color.clear)


归档时间:

查看次数:

3835 次

最近记录:

3 年,3 月 前