swiftswiftuiswift5

How to set the background color of a swiftui to lightGray?


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.


Solution

  • 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))