简体   繁体   中英

How can I change the List's row background color in SwiftUI WatchOS App

See preview screenshot here

I do not want my Image or Text to have a grey background like my buttons do. I am using a List so that the user can scroll down between the buttons.


struct ContentView: View {
  var body: some View {
    List {
      Image("Forest1")
        .resizable()
        .aspectRatio(contentMode: .fill)
        .padding(.all)
        .background(Color.black.ignoresSafeArea())
      Text("Welcome Back!")
        .padding()
        .background(Color.black.ignoresSafeArea())
      Button(action: PLACEHOLDER) {
        Text("Shake Tree")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Forge")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Cabin")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Map")
      }
    }
  }
}

You can use .listRowBackground(Color.black) to change the default list row background color to any color.


struct ContentView: View {
  var body: some View {
    List {
      Image("Forest1")
        .resizable()
        .aspectRatio(contentMode: .fill)
        .padding(.all)
        .background(Color.black.ignoresSafeArea())
        .listRowBackground(Color.black) // <- Here
      Text("Welcome Back!")
        .padding()
        .background(Color.black.ignoresSafeArea())
        .listRowBackground(Color.black) // <- Here
      Button(action: PLACEHOLDER) {
        Text("Shake Tree")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Forge")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Cabin")
      }
      Button(action: PLACEHOLDER) {
        Text("Open Map")
      }
    }
  }
}

预览截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM