[SwiftUI]リスト(List)の要素の背景色を透明にするには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

リストの要素の背景色を透明にする方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

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

コメント

タイトルとURLをコピーしました