swiftswiftuisearchbar

SwiftUI - Dismiss Searchbar


I am using the new SwiftUI searchbar .searchable. In conjunction with this, I am using a method that allows an action to be performed when search is clicked: .onSubmit(of: .search) {

The issue I am having is I cannot figure out how to dismiss the searchbar when .onSubmit is triggered.

the .onSubmit is working fine and I am able to call functions from inside of it, but cannot figure out how to dismiss the searchbar itself. The keyboard disappears, but the searchbar itself remains in an active state.

I have tried resorting to UIKit methods, but they do not seem compatible.

Any ideas?


Solution

  • There is a new environment value

    @Environment(\.dismissSearch) var dismissSearch
    

    that you can call as a function to dismiss the searchbar.