How to change Border Color of TextField in SwiftUI?

SwiftUI TextField – Change Border Color

To change border color of TextField, call border() method on this TextField, and pass the color value as argument.

Example

In the following example, we will change the border color of TextField to blue.

ContentView.swift

import SwiftUI

struct ContentView: View {
    @State private var username: String = ""
    
    var body: some View {
        TextField(
            "Enter..",
            text: $username
        )
        .padding(.all)
        .border(Color.blue)
        .padding(.all)
    }
}

Screenshot

Now, let us change the color to RGB specific values using Color.init() as shown in the following example. Provide values for red, green, blue and opacity in the range 0.0 to 1.0.

ContentView.swift

import SwiftUI

struct ContentView: View {
    @State private var username: String = ""
    
    var body: some View {
        TextField(
            "Enter...",
            text: $username
        )
        .padding(.all)
        .border(Color.init(Color.RGBColorSpace.sRGB,
                           red: 1.0,
                           green: 0.2,
                           blue: 0.4,
                           opacity: 1.0))
        .padding(.all)
    }
}

Screenshot

Summary

Summarising this SwiftUI Tutorial, we learned how to set border color for TextField control.