Swift リストの背景色を透明にするサンプル

環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
Ubuntu 20.04.2 LTS

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

作成方法
1.UITablView.appearance().backgroundColorに「.clear」を設定します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UITableView.appearance().backgroundColor = .clear
UITableView.appearance().backgroundColor = .clear
UITableView.appearance().backgroundColor = .clear

2.リストの要素にlistRowBackgroundColorを付与します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
List {
ItemView()
.listRowBackground(Color.clear)
}
List { ItemView() .listRowBackground(Color.clear) }
List {
    ItemView()
        .listRowBackground(Color.clear)
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
init () {
UITableView.appearance().backgroundColor = .clear
}
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.red.opacity(0.3))
List {
ForEach (1..<5, id: \.self) { index in
Text("\(index)")
.listRowBackground(Color.clear)
}
}
}
}
}
struct ContentView: View { init () { UITableView.appearance().backgroundColor = .clear } var body: some View { ZStack { Rectangle() .foregroundColor(.red.opacity(0.3)) List { ForEach (1..<5, id: \.self) { index in Text("\(index)") .listRowBackground(Color.clear) } } } } }
struct ContentView: View {
    init () {
        UITableView.appearance().backgroundColor = .clear
    }
    var body: some View {
        ZStack {
            Rectangle()
                .foregroundColor(.red.opacity(0.3))
            List {
                ForEach (1..<5, id: \.self) { index in
                    Text("\(index)")
                        .listRowBackground(Color.clear)
                }
            }
        }
    }
}

 

Swift

Posted by arkgame