Karim
Karim

Reputation: 291

Why doesn't the background of my TextField change to the colour I've set?

TextField remains white even when background colour is set

Why does the background of the TextField remain white after I've set it to use a defined colour? Why does it still overlay white?

import SwiftUI

struct ContentView: View {
    @State var search: String = ""

    var body: some View {
        ZStack{
            Color.baseColour
            .edgesIgnoringSafeArea(.all)

            VStack(alignment: .center, spacing: 0) {
                HStack(alignment: .center, spacing: 0) {
                    TextField("Search for a profile... ", text: $search)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .cornerRadius(15)
                        .background(Color.textFieldBarColour)
                }
                .frame(width: UIScreen.main.bounds.width - 30)
                .background(Color.textFieldBarColour)}

Upvotes: 2

Views: 1120

Answers (1)

Asperi
Asperi

Reputation: 258541

It is possible to do with plain textfield style, like below (your custom colours just replaced with known for demo)

demo

TextField("Search for a profile... ", text: $search)
    .textFieldStyle(PlainTextFieldStyle())
    .padding(8)
    .background(RoundedRectangle(cornerRadius: 15).fill(Color.yellow))

Upvotes: 2

Related Questions