UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Set Button highlight background color

Forums > SwiftUI

Is it possible? Can't find any mention of it

2      

Do you mean the colour behind the text or the text it self.

If text add modifier .foregroundColor(.blue)

if the colour behind the text add modifier .background(Color.blue) but would add .padding() first as it will not be big

if something else please be a bit more specfic.

2      

Thanks @NigelGee
My custom needs are a white button on a white background and when you press down the background changes.

2      

Do @State private var buttonTapped = false then modifier .background(buttonTapped ? Color.blue : Color.white) and in Button(action: put self.buttonTapped.toggle that should change the colour of button everytime you tap it.

PS you might need padding before the modifier

2      

Doesnt this tapped state act the same as the UIButton's selected state? I'm looking to mimic UIButton's highlighted state

2      

Maybe I need to either use UIButton Unfortunately cant introspect the button, as Button Not a UIButton

2      

struct PrimaryButtonStyle: ButtonStyle {
    let height: CGFloat = 60
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
           .font(.buttonLabel)
            .frame(maxWidth: .infinity, minHeight: height, maxHeight: height)
            .foregroundColor(.primaryButtonLabel)
            .background(configuration.isPressed ? Color.red : Color.yellow)
            .cornerRadius(.infinity)
    }
}

Solved it like this ^

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.