本文介绍了使可扩展列表的背景透明似乎是不可能的.在 StackOverflow 上测试了所有解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码
struct Lumba:Identifiable {
var id = UUID()
var name:String
var subLumba:[Lumba]?
}
struct ContentView: View {
let array = [
Lumba(name:"aaa", subLumba: [
Lumba(name: "a1", subLumba: nil), Lumba(name: "a3", subLumba: nil), Lumba(name:"a3", subLumba: nil)]),
Lumba(name:"bbb", subLumba: nil)
]
var body: some View {
ZStack {
List(array, children: .subLumba) { scrollItem in
Text(scrollItem.name)
.listRowBackground(Color.clear)
.background(Color.clear)
}
.listRowBackground(Color.clear)
}
.background(Color.red)
.onAppear() {
UITableView.appearance().separatorStyle = .none
UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().backgroundColor = UIColor.clear
}
}
}
这段代码包含了 Stack Overflow 上回答说要添加的内容,以使列表透明,以及大量冗余代码.
This code contains stuff that answers on Stack Overflow say to add, to make the list transparent, plus a lot of redundant code.
列表继续为白色.
StackOverflow 上的解决方案适用于简单列表但不可扩展.
The solutions on StackOverflow work for simple lists but not to expandable.
推荐答案
将你所有的 UITableViewCell.appearance().
移动到 init() 中并使用 ForEach
forlistRowBackground
Move your all the UITableViewCell.appearance().
inside the init() and use ForEach
for listRowBackground
struct ContentView: View {
let array = [
Lumba(name:"aaa", subLumba: [
Lumba(name: "a1", subLumba: nil), Lumba(name: "a3", subLumba: nil), Lumba(name:"a3", subLumba: nil)]),
Lumba(name:"bbb", subLumba: nil)
]
init() { //< -- Here
UITableView.appearance().separatorStyle = .none
UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().backgroundColor = UIColor.clear
}
var body: some View {
ZStack {
List { //< -- Here
ForEach(array, id: .id) { scrollItem in //< -- Here
Text(scrollItem.name)
}
.listRowBackground(Color.clear) //< -- Here
}
}
.background(Color.red)
}
}
已测试:XCode 12.3、iOS 14.3
Tested: XCode 12.3, iOS 14.3
这篇关于使可扩展列表的背景透明似乎是不可能的.在 StackOverflow 上测试了所有解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!