Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

swiftui - It appears impossible to make the background of an expandable list transparent. Tested all solutions on StackOverflow

This is the code

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
    }
  }
}

This code contains stuff that answers on Stack Overflow say to add, to make the list transparent, plus a lot of redundant code.

The list continues white.

The solutions on StackOverflow work for simple lists but not to expandable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
    }
}

Tested: XCode 12.3, iOS 14.3

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...