Skip to content

Instantly share code, notes, and snippets.

@yannxou
Created January 20, 2023 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannxou/90471044a45167a4c1568308f7553323 to your computer and use it in GitHub Desktop.
Save yannxou/90471044a45167a4c1568308f7553323 to your computer and use it in GitHub Desktop.
Pass FocusState property to child views
// source: https://developer.apple.com/forums/thread/682448
struct Parent: View {
@FocusState var focusedField: UUID?
var body: some View {
VStack {
Child(focusedField: _focusedField)
}
}
}
struct Child: View {
@FocusState var focusedField: UUID?
@State var someText: String = ""
@State var someTextFieldUUID: UUID = UUID()
var body: some View {
VStack {
TextField("Focusable field", text: $someText)
.focused($focusedField, equals: someTextFieldUUID)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment