どうも、ちょげ(@chogetarou)です。
「リストの背景色を設定したのに色が変わらない」
という人に向けて、Listの背景色が変わらない時の対処方法を紹介します。
方法

Listの背景色が変わらない場合には、listRowBackground修飾子を使います。
まず、Listの要素にlistRowBackground修飾子を付与します。
そして、listRowBackground修飾子の引数に「Color.clear」を指定します。
List {
ItemView()
.listRowBackground(Color.clear)
}
listRowBackground(Color.clear)を指定することで、Listの要素の背景が透明になります。
リストの要素の背景色が透明になることで、リスト自体の背景色が変わるようになります。
使用例
以下は、使用例です。

struct ContentView: View {
init () {
UITableView.appearance().backgroundColor = .clear
}
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.green.opacity(0.2))
List {
ForEach (1..<6, id: \.self) { index in
Text("\(index)")
.listRowBackground(Color.clear)
}
}
}
}
}
コメント