SwiftUIでリストの背景色を変更するにはどうすればよいですか?質問する

SwiftUIでリストの背景色を変更するにはどうすればよいですか?質問する

UIKit で構築した UI を SwiftUI で再作成しようとしていますが、いくつか小さな問題が発生しています。

ここの色を変更したいのですListが、期待どおりに機能するプロパティがないようです。サンプル コードを以下に示します。

struct ListView: View {
    @EnvironmentObject var listData: ListData

       var body: some View {
        NavigationView {
            List(listData.items) { item in
                ListItemCell(item: item)
            }
            .content.background(Color.yellow) // not sure what content is defined as here
            .background(Image("paper-3")) // this is the entire screen 
        }
    }
}

struct ListItemCell: View {
    let item: ListItem

    var body: some View {

        NavigationButton(destination: Text(item.name)) {
            Text("\(item.name) ........................................................................................................................................................................................................")
                .background(Color.red) // not the area I'm looking for
        }.background(Color.blue) // also not the area I'm looking for
    }
}

白い部分を変えたい

ベストアンサー1

リストの行に色を付ける解決策を見つけました:

struct TestRow: View {

    var body: some View {
        Text("This is a row!")
        .listRowBackground(Color.green)
    }
}

そして本文では:

List {
    TestRow()
    TestRow()
    TestRow()
}

これは期待どおりに動作しますが、行間の区切り線を削除する方法はまだわかりません...

おすすめ記事