โ†

SwiftUI: Autofocus on a textfield and show the keyboard

This works from iOS 15.

Make an enum listing all the possible states of the focus: enum Field: Int, Hashable { case someField } and a variable with that enum: @FocusState var focusField: Field?.

Then apply each focus state to the TextFields or TextEditors etc using the .focused modifier .focused($focusField, equals: .someField).

Then on focus on that as soon as the view appears: .onAppear { focusField = .someField }

swiftui
I've removed disqus comments
Instead, press this
to express thanks
So far, 0 people have pressed the octopus
I'm no longer visiting the Reddit tabloid. It's anger-inducing and divisive
I'm writing small thoughts here