<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/focused(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE7focusedyQrAA10FocusStateV7BindingVySb_GF"
  },
  "title" : "focused(_:)"
}
-->

# focused(_:)

Modifies this view by binding its focus state to the given Boolean state
value.

```
nonisolated func focused(_ condition: FocusState<Bool>.Binding) -> some View
```

## Parameters

`condition`

The focus state to bind. When focus moves
to the view, the binding sets the bound value to `true`. If a caller
sets the value to  `true` programmatically, then focus moves to the
modified view. When focus leaves the modified view, the binding
sets the value to `false`. If a caller sets the value to `false`,
SwiftUI automatically dismisses focus.

## Return Value

The modified view.

## Discussion

Use this modifier to cause the view to receive focus whenever the
`condition` value is `true`. You can use this modifier to
observe the focus state of a single view, or programmatically set and
remove focus from the view.

In the following example, a single [`TextField`](/documentation/SwiftUI/TextField) accepts a user’s
desired `username`. The text field binds its focus state to the
Boolean value `usernameFieldIsFocused`. A “Submit” button’s action
verifies whether the name is available. If the name is unavailable, the
button sets `usernameFieldIsFocused` to `true`, which causes focus to
return to the text field, so the user can enter a different name.

```
@State private var username: String = ""
@FocusState private var usernameFieldIsFocused: Bool
@State private var showUsernameTaken = false

var body: some View {
    VStack {
        TextField("Choose a username.", text: $username)
            .focused($usernameFieldIsFocused)
        if showUsernameTaken {
            Text("That username is taken. Please choose another.")
        }
        Button("Submit") {
            showUsernameTaken = false
            if !isUserNameAvailable(username: username) {
                usernameFieldIsFocused = true
                showUsernameTaken = true
            }
        }
    }
}
```

To control focus by matching a value, use the
[`focused(_:equals:)`](/documentation/SwiftUI/View/focused(_:equals:)) method instead.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)