Estoy creando una aplicación iOS simple con SwiftUI y me gustaría cambiar el color de fondo de mi vista cuando cambie el interruptor. 
Mi código
struct ContentView: View { @State private var isOnLight: Bool = false var body: some View { VStack { Toggle(isOn: $isOnLight) { Text("Switch") .font(.title) .foregroundColor(.gray) } if isOnLight { } }.padding() } }Para los colores de fondo, puede usar el ZStack de esta manera y con una línea, luego decida el color.
struct ContentView: View { @State private var isOnLight: Bool = false var body: some View { ZStack { isOnLight ? Color.blue : Color.red VStack { Toggle(isOn: $isOnLight) { Text("Switch") .font(.title) .foregroundColor(.gray) } } .padding() } } }Para obtener información sobre cómo usar el operador ternario en SwiftUI, puede ver este video
Solo necesita incrustar su VStack dentro de un ZStack , donde la capa posterior es un color que cambia cada vez que cambia isOnLight .
Me gusta esto:
struct Example: View { @State private var isOnLight: Bool = false @State private var color: Color = .white var body: some View { ZStack { color .ignoresSafeArea() VStack { Toggle(isOn: $isOnLight) { Text("Switch") .font(.title) .foregroundColor(.gray) } } .padding() } .onChange(of: isOnLight) { value in if value { color = .yellow } else { color = .white } } } }