1

I am trying to change color of textfield with below code but it is not working. It takes color as white if the it is light theme and for dark theme automatically takes black color. I want to keep it always black.

    var body: some View {

    TextField(self.textFieldTitle ?? "", text: self.$textFieldInput, onEditingChanged: { editing in self.dosomeCode()}, onCommit: { self.someCode()})
                .background(Color.green)
                .foregroundColor(Color.white)
}
0

2 Answers 2

1

I want to keep it always black.

Use .black color instead

TextField(self.textFieldTitle ?? "", text: self.$textFieldInput, onEditingChanged: { editing in self.dosomeCode()}, onCommit: { self.someCode()})
            .background(Color.green)
            .foregroundColor(Color.black)  // << here !!

demo1demo2

Tested with Xcode 13.2 / iOS 15.2

Sign up to request clarification or add additional context in comments.

Comments

1

You can set the preferredColorScheme to light for this TextField in order to keep its color scheme same on all mode like this

TextField("Title", text: $title)
        .preferredColorScheme(.light) //ColorScheme
        .background(Color.green)
        .foregroundColor(Color.white)

1 Comment

somehow This change the whole view to only .light theme instead of just textfield

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.