SwiftUI TextField: Background Color


TextField view doesn’t provide a way to add background color but we can use the background modifier to add color to the TextField.
struct TextFieldExample: View {
    
    @State private var title: String = ""
    @State private var category: String = ""
    @State private var type: String = ""
    
    var body: some View {
        VStack {
            Text("DevTechie Courses")
                .font(.largeTitle)
            
            VStack(alignment: .leading) {
                Text("Enter new course title")
                    .font(.title3)
                
                TextField("Course title", text: $title)
                    .padding(5)
                    .background(Color.orange)
                    
                TextField("Course category", text: $category)
                    .padding(5)
                    .background(Color.teal)
                    
                TextField("Course type", text: $type)
                    .padding(5)
                    .background(Color.pink.opacity(0.2))
                
            }.padding(.top, 20)
        }.padding()
    }
}