SwiftUI First Responder - TextField

Hi - are you able to set first responder now in SwiftUI 2?
Thanks!
Nick
I have not seen anything in the documentation suggesting SwiftUI 2 supports setting the first responder unfortunately.
No, this is not supported in a general way, though watchOS and tvOS have new view modifiers for influencing which focusable view receives focus by default. To use it, you need to define a namespace with the Namespace property wrapper and then pass along the namespace value to the focusScope(_:) and prefersDefaultFocus(in:) modifiers.

Code Block Swift
struct ContentView: View {
    @State var message = "Hello, SwiftUI!"
    @Namespace var exampleNamespace
    var body: some View {
        VStack {
            Button("Do Something", action: {...})
            TextField("Type Something", text: $message)
// Available on watchOS and tvOS
                .prefersDefaultFocus(in: exampleNamespace)
        }
// Available on watchOS and tvOS
        .focusScope(exampleNamespace)
    }
}

When can we expect the ability to set the first responder, particularly in iOS and iPadOS? Unfortunately this could be a blocker for me moving to SwiftUI. I haven't found any workaround for this yet.
Will it be possible to add an own toolbar, custom buttons or similar above the keyboard? 
Will it be possible to automatically shrink the current view when an keyboard enters the display (like in a VStack). 
I really wish be could do more with the keyboard, since it is an essential element of the user experience.
Setting the focus/firstResponder on a control improves the user experience and is in my eyes absolutely essential. I expected that SwiftUI will support this from now on. Very disappointing, that it doesn't do it. But there is some time until the final release and I hope that Apple will listen to our wishes. I would be very happy if SwiftUI would support it. Would be nice to do something like this:

Code Block
struct ContentView: View {
@Environment(\.firstResponder) private var firstResponder: ObjectIdentifier
@State var text1: String = ""
@State var text2: String = ""
var body: some View {
VStack {
TextField("...", text: self.$text1)
.id("TF1")
TextField("...", text: self.$text2)
.id("TF2")
HStack {
Button("Focus 1") {
self.firstResponder = "TF1"
}
Button("Focus 2") {
self.firstResponder = "TF2"
}
}
}
}
}


That watchOS/tvOS example is exactly what I think is necessary across the board. I've filed a feedback on this (FB7783891) -- to any other developers who came across this, please file your own Feedback, as this can influence feature sets, especially in volume!
I've also filed a feedback. Hopefully a lot of people are doing this too.
I don't know why they didn't add it this year. From my point of view, this was the first essential missing thing I came across in SwiftUI.
I struggled with this missing in SwiftUI 1 but chalked it up to it being brand new. I must say that I am very surprised it is still missing in SwiftUI 2. Feedback filed.
In addition we need a way to access the keyboard Next return key, and way to move through the fields when Next is selected.
We have a new open source SwiftUI package for the needs, at https://github.com/mobilinked/MbSwiftUIFirstResponder
  1. The SwiftUI style APIs

TextField("Name", text: $name)
.firstResponder(id: FirstResponders.name, firstResponder: $firstResponder, resignableUserOperations: .all)


TextEditor(text: $notes)
.firstResponder(id: FirstResponders.notes, firstResponder: $firstResponder, resignableUserOperations: .all)

2. Option for "tap to resign" for iOS
3. Option for "click to resign", "return key, esc key, ... to resign" for macOS

It almost meets all the needs in the replies.

New for SwiftUI 3

Many change-the-first-responder situations can be handled with the new Focus State API: https://developer.apple.com/documentation/swiftui/focusstate

The Direct and Reflect Focus in SwiftUI talk goes into the API in a bit more detail: https://developer.apple.com/videos/play/wwdc2021/10023/

SwiftUI First Responder - TextField
 
 
Q