-3

enter image description here

I want to make a simple form and change the background color of a text field, but Xcode provide me .background(background: View), etc option but not .background(Color()).

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
subhangi pawar
  • 451
  • 2
  • 7
  • 16

3 Answers3

5

Color is conformed to View. So you can use it like any other View. The issue with your code is you add cornerRadius inside the background modifier. It should be out like this:

TextField("Title", text: .constant("text"))
    .background(Color.red)
    .cornerRadius(5)
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
3

Try below code.

struct ContentView: View {

   @State var name: String = ""

   var body: some View {

       VStack(alignment: .leading, spacing: 10) {

           Text("NAME").font(.headline)

           TextField("Enter some text", text: $name)
           .padding(.horizontal , 15)
           .frame(height: 40.0)
           .background(Color(red: 239/255, green: 243/255, blue: 244/255))
           .overlay(
               RoundedRectangle(cornerRadius: 5)
                   .stroke(Color.gray.opacity(0.3), lineWidth: 1)
           )
       }.padding(.horizontal , 15)
   }
}
Rohit Makwana
  • 4,337
  • 1
  • 21
  • 29
-2
import SwiftUI

struct ContentView: View {

    @State var name: String = ""

    var body: some View {
        VStack {
            TextField("Enter some text", text: $name)
                .background(Color.red)
        }
    }
}
Andrew
  • 26,706
  • 9
  • 85
  • 101