Understanding @FocusState in SwiftUI

SwiftUI uses the @FocusState property wrapper to keep track of a view’s focus state. The active view is the one that gets keyboard input and other focus-related events when a view is focused.

Here is an illustration of how you could modify the appearance of a view depending on whether it is focused or not using the @FocusState property wrapper:

struct YourView: View {
    @FocusState var isFocused: Bool

    var body: some YourView {
        Text("Your View")
            .foregroundColor(isFocused ? .yellow : .red)
    }
}

This example shows how to track the focus status of a view using the @FocusState property wrapper and how to use the isFocused property to alter the text color of a view depending on whether it is focused or not. The @FocusState property wrapper allows you to keep track of a view’s focus status and modify the view’s appearance or behavior depending on whether it is focused or not.

Another example using FocusState can be seen here: