2

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)}
sfung3
  • 2,227
  • 1
  • 9
  • 30
Karim
  • 271
  • 2
  • 11
  • 1
    You must post the code and not an image on StackOverflow. Look up this answer, it says the same and you might need to use .overlay() https://stackoverflow.com/questions/58727341/change-the-background-colour-of-a-textfield-in-swiftui-with-xcode11-1 – Roland Lariotte Mar 06 '20 at 23:54

1 Answers1

2

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))
Asperi
  • 228,894
  • 20
  • 464
  • 690