Hello, I recently encountered the same problem as you when developing notebook tools in the program. I've also been looking for ways to use SwiftUI to solve the problem, but it seems that I can only use UIKit for now. I found a very convenient way to deal with it, I hope it can help you:
struct xxxView: View {
...
.navigationBarItems(trailing:
Button(action: {
UIApplication.shared.endEditing()
}) {
Text("Finished")
}
)
...
}
...
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
Source of this solution: https://stackoverflow.com/questions/58349955/swiftui-dismiss-keyboard-when-tapping-segmentedcontrol