Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smartest way to set color for enabled/disabled buttons with SwiftUI

Tags:

button

swiftui

I'd like to set the color of a Text view inside a Button view based on whether the button is enabled or disabled. What is the smartest way to do this and also to get the system default colors for enabled/disabled colors, because I don't want to set any gray shade, I'd like to set the "normal" gray shade.

like image 238
G. Marc Avatar asked Oct 22 '25 12:10

G. Marc


2 Answers

Actually, adding .buttonStyle(.plain) to the button just did what I was looking for. Now, the button automatically gets a gray color if it's disabled.

like image 81
G. Marc Avatar answered Oct 24 '25 03:10

G. Marc


Here is how to do it in SwiftUI 2.0

Button("PressME", action: {self.playerAttemptCount += 1
                           }).buttonStyle(PlainButtonStyle())

This way, the button automatically gets a gray color if it is disabled.

like image 26
Heyman Avatar answered Oct 24 '25 01:10

Heyman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!