回答編集履歴

1

ContentViewの背景色を変えるコードを追記しました。

2023/04/17 07:00

投稿

退会済みユーザー
test CHANGED
@@ -47,3 +47,70 @@
47
47
  }
48
48
  ```
49
49
 
50
+
51
+ ## 追記です
52
+
53
+ コメントありがとうございます。
54
+
55
+ > 何度やってもリストロウの背景色だけが変化しないので、
56
+
57
+ ContentViewの背景色が
58
+ NextViewの背景色と同じになるように修正してみました。
59
+
60
+ ```swift
61
+ import SwiftUI
62
+
63
+ struct ContentView: View {
64
+ let list = ["a", "b", "c"]
65
+ @State var backcolor: Bool = false
66
+ var body: some View {
67
+ NavigationView {
68
+ ZStack {
69
+ (backcolor ? Color.black : Color.white)
70
+ .ignoresSafeArea()
71
+ VStack {
72
+ List {
73
+ Section {
74
+ ForEach(list, id: \.self) { text in
75
+ NavigationLink {
76
+ NextView(backcolor: $backcolor)
77
+ } label: {
78
+ Text("テキスト")
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ .scrollContentBackground(.hidden)
87
+ }
88
+ }
89
+
90
+ struct NextView: View {
91
+ @Binding var backcolor: Bool
92
+ var body: some View {
93
+ List {
94
+ Section(header: Text("背景色")
95
+ .listRowBackground(Rectangle()
96
+ .background(Color.clear)
97
+ .foregroundColor(backcolor ? Color.gray : Color.white)
98
+ .opacity(0.3))
99
+ .foregroundColor(backcolor ? Color.white : Color.black)) {
100
+ Toggle(isOn: $backcolor) { }
101
+ }
102
+ }
103
+ .scrollContentBackground(.hidden)
104
+ .background(backcolor ? Color.black : Color.white)
105
+ }
106
+ }
107
+ ```
108
+
109
+ 次のリンクなども見てみたのですが、
110
+ NavigationViewの背景色を設定するのは
111
+ 色々問題がありそうなのですね。
112
+
113
+ [ios - How to change background color of the NavigationView in SwiftUI? - Stack Overflow](https://stackoverflow.com/questions/57685679/how-to-change-background-color-of-the-navigationview-in-swiftui)
114
+ [ios - How To Set NavigationView Background Colour in SwiftUI - Stack Overflow](https://stackoverflow.com/questions/57508983/how-to-set-navigationview-background-colour-in-swiftui)
115
+ [swift - How change background color if using NavigationView in SwiftUI? - Stack Overflow](https://stackoverflow.com/questions/56923397/how-change-background-color-if-using-navigationview-in-swiftui)
116
+