Hi, I have a text field for storing the name of a user on their profile. I have an edit button to the right of the text field that enables editing so the user doesn't accidentally change their profile. However, when I enable editing, it just enables the text field but doesn't have the user start typing in it (i.e. bring up the keyboard so that they can edit the text field).
Here is my code:
struct ProfilePage: View {
@State private var name = ""
@State var nameEditEnabled = false
@AppStorage("NAME_KEY") var savedName = ""
var body: some View {
VStack {
HStack {
TextField("Name", text: $savedName)
.textFieldStyle(.roundedBorder)
.font(.title) .disableAutocorrection(true)
.onChange(of: name) {text in
self.savedName = name }
.onSubmit { nameEditEnabled = false }
.disabled(nameEditEnabled ? false : true)
Button(action: {self.nameEditEnabled.toggle()}) {
Image(systemName: "square.and.pencil")
}
}
}
.padding()
}
}
How would I go about making it so that when the button is pressed, the user automatically is put into the textfield and the keyboard pops up so that they can begin typing (without having to manually press the textfield)?
Thanks.