I've submitted this as FB13628591 but I figure a forum post never hurts ;)
I’ve discovered an issue with keyboard presentation when using .searchable in SwiftUI in iOS 17.4 beta 3, shown in this video (https://imgur.com/1hmM5u0) running this sample code.
struct Animal: Identifiable {
var id: String {
return name
}
let name: String
}
struct ContentView: View {
let animals: [Animal] = [.init(name: "Dog"), .init(name: "Cat"), .init(name: "Turtle")]
@State var presentedAnimal: Animal? = nil
@State var searchText: String = ""
var body: some View {
NavigationStack {
List {
ForEach(animals) { item in
Button(item.name) {
self.presentedAnimal = item
}
}
}
.sheet(item: $presentedAnimal) { item in
Text(item.name)
}
.searchable(text: $searchText)
}
}
}
As of iOS 17.4, the behavior of the above code is that if you tap one of the list buttons to present a sheet while the keyboard is still presented, the keyboard will dismiss then represent once the sheet is presented. In the video, I tap the “Cat” button while the search keyboard is still presented. This is confusing, because the keyboard actually belongs to the presenting .searchable view, not the presented sheet. In all previous versions, the keyboard would dismiss and stay that way. That is the correct behavior.
I’ve noticed this erroneously presented keyboard does not respond to calls to resignFirstResponder:
let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)