Return Value 戻り値
The modified view.
Availability 有効性
Technology
func focused<Value>(_ binding: FocusState
<Value>.Binding
, equals value: Value) -> some View
where Value : Hashable
The modified view.
binding
The state binding to register. When focus moves to the modified view, the binding sets the bound value to the corresponding match value. If a caller sets the state value programmatically to the matching value, then focus moves to the modified view. When focus leaves the modified view, the binding sets the bound value to nil
. If a caller sets the value to nil
, SwiftUI automatically dismisses focus.
value
The value to match against when determining whether the binding should change.
Use this modifier to cause the view to receive focus whenever the the binding
equals the value
. Typically, you create an enumeration of fields that may receive focus, bind an instance of this enumeration, and assign its cases to focusable views.
The following example uses the cases of a Login
enumeration to bind the focus state of two Text
views. A sign-in button validates the fields and sets the bound focused
value to any field that requires the user to correct a problem.
struct LoginForm {
enum Field: Hashable {
case usernameField
case passwordField
}
private var username = ""
private var password = ""
private var focusedField: Field?
var body: some View {
Form {
TextField("Username", text: $username)
.focused($focusedField, equals: .usernameField)
SecureField("Password", text: $password)
.focused($focusedField, equals: .passwordField)
Button("Sign In") {
if username.isEmpty {
focusedField = .usernameField
} else if password.isEmpty {
focusedField = .passwordField
} else {
handleLogin(username, password)
}
}
}
}
}
To control focus using a Boolean, use the focused(_:)
method instead.