背景色を変える

以下のように、背景色を変数にしておく。

struct KeyboardPad: View {
    @State var backgroundColor: Color

    var body: some View {
        ZStack {
            self.backgroundColor.edgesIgnoringSafeArea(.all)
            Image("keyboard_background_image", bundle: nil)
                .frame(width: UIScreen.main.bounds.width, height: nil, alignment: .center)
                .opacity(0.5)
        }
    }
}

そして、以下のメソッドを呼び出して、背景色の変数を変える。

func changeBackground() {
    self.backgroundColor = Color.blue
}

こうすることで、タップアクションなどで動的に背景色を変えられる。
これをもとに、文字を動的に変えたり、画像を切り替えたりすることも可能だろう。