1337gamerman
1337gamerman

Reputation: 75

Set background color with button

How would I go about setting the background of the view with a button?

I would assume you would do something along the lines of

struct ContentView: View {
    var body: some View {
        Button(
            action: {
                self.foregroundColor(Color.red)
        }
            )
        {
            Text("Change Color")
        }
    }
}

But no matter what way I write it, it never works

Any help is appreciated

Upvotes: 1

Views: 35

Answers (1)

Asperi
Asperi

Reputation: 258413

Here it is

@State var myColor: Color = .black
var body: some View {
    Button(
        action: {
            self.myColor = Color.red
    }
        )
    {
        Text("Change Color")
            .foregroundColor(myColor)
    }
}

Upvotes: 1

Related Questions