Reputation: 3631
I am trying to change colour of my Button
in SwiftUI.
This is my whole CustomButton view struct:
struct CustomButton: View {
@State private var didTap:Bool = false
var body: some View {
Button(action: {
self.didTap = true
}) {
Text("My custom button")
.font(.system(size: 24))
}
.frame(width: 300, height: 75, alignment: .center)
.padding(.all, 20)
.background(Color.yellow)
//My code above this comment works fine
//I tried something like this, but it is not working
// if didTap == true {
// .background(Color.blue)
// }
}
}
This is what my button looks like (which is fine):
But my question is: how can I change the background colour when user taps this button.
Thank you.
Upvotes: 18
Views: 30540
Reputation: 1
Did this a while ago based on a quiz making app. If the user got an answer correct, it would turn a button green, if wrong, turns the button red. The same could work for just a clickable button as well by removing the elif statement.
@IBAction func answerButtonPressed(_ sender: UIButton) {
let actualAnswer = quiz[questionNumber].a
let buttonPressed = sender.currentTitle!
if buttonPressed == actualAnswer {
print("Right")
sender.backgroundColor = UIColor.green
} else {
print("wrong")
sender.backgroundColor = UIColor.red
}
Upvotes: -3
Reputation: 34
But in the new Ways, or in the new version of Swift, Xcode, and iOS, you just have to write .color_name
, if you will use class, like above, then it will generate errors.
In the Struct class just add in the ContentView: View
:
@State private var didTap:Bool = false
Button(action:{
self.didTap = true
}) {
Text("ಕನ್ನಡ").font(.system(size: 17))
.fontWeight(.bold)
.foregroundColor(didTap ? .orange :.black)
.frame(minWidth: 0, maxWidth:143)
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
Upvotes: 0
Reputation: 3631
Just in case somebody wanted different way of doing this. It works for more colors.
struct CustomButton: View {
@State private var buttonBackColor:Color = .yellow
var body: some View {
Button(action: {
//This changes colors to three different colors.
//Just in case you wanted more than two colors.
if (self.buttonBackColor == .yellow) {
self.buttonBackColor = .blue
} else if self.buttonBackColor == .blue {
self.buttonBackColor = .green
} else {
self.buttonBackColor = .yellow
}
//Same code using switch
/*
switch self.buttonBackColor {
case .yellow:
self.buttonBackColor = .blue
case .blue:
self.buttonBackColor = .green
default:
self.buttonBackColor = .yellow
}
*/
}) {
Text("My custom button")
.font(.system(size: 24))
}
.frame(width: 300, height: 75, alignment: .center)
.padding(.all, 20)
.background(buttonBackColor)
}
}
Upvotes: 7
Reputation: 244
For doinng this you need to give the colour as per the state changed:
struct CustomButton: View {
@State private var didTap:Bool = false
var body: some View {
Button(action: {
self.didTap = true
}) {
Text("My custom button")
.font(.system(size: 24))
}
.frame(width: 300, height: 75, alignment: .center)
.padding(.all, 20)
.background(didTap ? Color.blue : Color.yellow)
}
}
PS: If you want to manage other states too then you can go for the enum
.
Upvotes: 22