首页 > 解决方案 > 如何在 Swift 中使列表完全透明?

问题描述

我是 SwiftUI 初学者。我在 SwiftUI 应用程序中根据需要创建列表时遇到问题!

这是我的代码:

struct Search: View {

@ObservedObject var webservice = Webservice()

@State var depart = "Gent-Sint-Pieters"

@State var destination = "Mechelen"

var body: some View {

    VStack (spacing: 20){

        Rechercher(depart: $depart , destination: $destination)       

        HStack {

                List(webservice.rides, id:\.id) { item in

                    Text("")

                }

                //.colorMultiply(Color(hue: 0.547, saturation: 0.071, brightness: 1.0))

            Spacer()
        }

        RechercheButton()
    }
}
}

我想要的是删除列表的白色背景。可能吗 ?

你能告诉我我需要在这段代码中添加什么以获得这样的结果(每个灰色分隔线之间有一个内容,并且列表没有背景颜色)吗?

在此处输入图像描述

谢谢你 !!

标签: swiftxcodeswiftuiswift5

解决方案


在视图的 init 方法中设置清除颜色

init() {
        UITableView.appearance().backgroundColor = .clear 
        UITableViewCell.appearance().backgroundColor = .clear
    }

因此,您的答案将如下所示

struct Search: View {

@ObservedObject var webservice = Webservice()

@State var depart = "Gent-Sint-Pieters"

@State var destination = "Mechelen"

init() {
            UITableView.appearance().backgroundColor = .clear 
            UITableViewCell.appearance().backgroundColor = .clear
        }

var body: some View {

    VStack (spacing: 20){

        Rechercher(depart: $depart , destination: $destination)       

        HStack {

                List(webservice.rides, id:\.id) { item in

                    Text("")

                }

                //.colorMultiply(Color(hue: 0.547, saturation: 0.071, brightness: 1.0))

            Spacer()
        }

        RechercheButton()
    }
}
}

推荐阅读