I've got a SwiftUI TextEditor
that appears once a button is pressed. What I can't figure out is, how to make the TextEditor
the first responder when it appears.
What I've tried is adding a .focused
modifier to the TextEditor
and then setting the focused Bool value to true
inside .onAppear
. But still the keyboard only shows up when its pressed.
import SwiftUI
struct ContentView: View {
@State var showTextField : Bool = false
@State private var currentEditText : String = "Some text"
@FocusState private var editTextFieldFocus: Bool
var body: some View {
VStack {
Button {
showTextField.toggle()
} label: {
Text("Show Text Field")
}
Text("Hello, world!")
.padding()
if showTextField{
TextEditor(text: $currentEditText)
.focused($editTextFieldFocus)
.onAppear {
editTextFieldFocus = true
}
}
}
}
}