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

SwiftUI

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

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

スポンサーリンク

方法

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

Listの背景色を透明にするには、UITablViewとlistRowBackgroundColorを使います。

まず、UITablView.appearance().backgroundColorに「.clear」を代入します。

UITableView.appearance().backgroundColor = .clear

そして、リストの要素にlistRowBackgroundColorを付与し、引数に「Color.clear」を指定します。

List {
    ItemView()
        .listRowBackground(Color.clear)
}

これでListの背景が透明になります。

リストの背景色自体は、UITableView.appearance().backgroundColorで設定できます。

ですが、リストの要素の背景色によって、白のままになります。

なので、listRowBackgroundで要素の背景色も透明にします。

使用例

以下は、使用例です。

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をコピーしました