I filled out a bug report. For me it's neither working right now. As workaround you can use something like this:
@available(iOS 15, *)
struct FocusModifier: ViewModifier {
@FocusState var focused: Bool
@Binding var state: Bool
init(_ state: Binding<Bool>){
self._state = state
}
func body(content: Content) -> some View {
content.focused($focused, equals: true)
.onChange(of: state, perform: changeFocus)
}
private func changeFocus(_ value: Bool){
focused = value
}
}
@available(iOS 15, *)
extension View{
func focusMe(state: Binding<Bool>) -> some View {
self.modifier(FocusModifier(state))
}
}
struct ContentView: View {
@State var text = ""
@available(iOS 15, *)
@State var focus = false
var body: some View{
VStack{
if #available(iOS 15, *) {
TextField("Type here", text: $text)
.focusMe(state: $focus)
Button(action: {focus.toggle()}) {
Text("Change Focus")
}
}else{
TextField("Type here", text: $text)
}
}
}
}
Post
Replies
Boosts
Views
Activity
I'm not sure what exactly you searching for. This is the SwiftUI iOS 15 way:
TextField("Placeholder", text: $text, onCommit: {
// Add Text to Array
}).submitLabel(.done)