Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 521
Damion Silver Avatar asked Dec 03 '19 03:12

Damion Silver


4 Answers

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))
like image 129
fulvio Avatar answered Oct 30 '22 06:10

fulvio


Can also use .background(Color(.systemGray6))

like image 31
adamwjohnson5 Avatar answered Oct 30 '22 05:10

adamwjohnson5


I also sometimes adjust the color of Gray like so:

.background(
    Color.gray
        .brightness(0.4)
)

or

.background(
    Color.gray
        .opacity(0.1)
)
like image 27
Ryan Avatar answered Oct 30 '22 05:10

Ryan


There is no lightGray color in Color, but you can use .background(Color.init(UIColor.lightGray))

like image 28
Hrabovskyi Oleksandr Avatar answered Oct 30 '22 04:10

Hrabovskyi Oleksandr