在SwiftUI中为表单节清除背景吗? [英] Clear background for form sections in SwiftUI?

查看:72
本文介绍了在SwiftUI中为表单节清除背景吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to remove the white background of some sections so the elements lay right on the grey background, but I can't get the section background to be removed or transparent.

这是我正在尝试的:

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中如何实现?

I tried to add .background(Color.clear) to the Section and VStack, but it did not have any effect. How an this be achieved in SwiftUI?

推荐答案

即使在SwiftUI 2中,Form也是基于UIKit构建的,特别是UITableView.

Even in SwiftUI 2 Form is built on top of UIKit, specifically UITableView.

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

You need to remove the default UITableViewCell's background (only once, preferably in the App init):

UITableViewCell.appearance().backgroundColor = UIColor.clear

,并使用以下背景更改背景:

and change the background using:

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

这篇关于在SwiftUI中为表单节清除背景吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆