I am currently trying to set the background color of a text field in SwiftUI to light gray. it is not working
my code is this
TextField("Enter Your Email", text: $username)
.background(Color.lightGray)
Any idea what could be wrong with it ? It works when i just use "gray" But i need the color to be lighter than that. I looked up lightGray in swift , and it says it is a color.
You can set the background by adding .padding()
to the TextField
and then using UIColor.lightGray
. Setting the .padding()
isn't required, however it makes it look a little more native when setting a background color.
TextField("Enter Your Email", text: $text)
.padding()
.background(Color(UIColor.lightGray))
Can also use .background(Color(.systemGray6))
I also sometimes adjust the color of Gray like so:
.background(
Color.gray
.brightness(0.4)
)
or
.background(
Color.gray
.opacity(0.1)
)
There is no lightGray
color in Color
, but you can use .background(Color.init(UIColor.lightGray))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With