In iOS 17.4 beta 3, .searchable incorrectly presents keyboard when presenting a sheet

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)

Oh hey, I found that moving the .sheet to the NavigationStack rather than the List brings the correct presentation behavior back. Maybe that was always best practice? In any case, this behavior changed in the 17.4 beta and is still present as of beta 4.

I'm unable to move the .sheet(), but I can somewhat circumvent this by using .disable() on .searchable() when the sheet is presented.

Adding here that this is still a bug in iOS 17.4.1 and for me what solved it is moving my .sheet from being a modifier on my Section to be a on the Button that triggers the change to that @State var.

Apple, is SwiftUI ever going to make sense? Every new iOS that comes out there are different bugs with SwiftUI.

UIKit was much more stable than that

Same here.

Both of the above workarounds fix the problem of the keyboard appearing over the sheet, but there is still another problem that causes a layout change that seems to temporarily cancel the search bar when the sheet is displayed.

This is not much of an issue when the sheet is displayed in full screen, but it is quite annoying when the sheet is displayed in PresentationDetent.medium, etc.

I have not found a workaround for this problem yet.

Same issue here on iOS 17.5.1

Can we hope for an iOS 18 fix?

In iOS 17.4 beta 3, .searchable incorrectly presents keyboard when presenting a sheet
 
 
Q