Once a search bar is being displayed, you may be unaware that there is a way to dismiss it. There is a Cancel button that appears next to the search bar, but what if you want to dismiss the search bar manually. It is possible to get the environment value that contains a type called a DismissSearchAction
, and this can be called as a function although it isn’t one.
Now I have a DismissButton
and IdentifiableData
I can display in a List
. To draw attention to the limitations of DismissSearchAction
, I have a DismissButton
at the top of the VStack
. Above it I have a Text
that tells you that it won’t work, because the DismissSearchAction
value is only passed to views that have the .searchable
modifier applied.
The DismissButton
is displayed in every Section
of the List
.
Although no Section
has the .searchable
modifier, the List
does, and that’s all that is needed.
Pressing the search button on the iOS software keyboard will filter the list, but the DismissButton
will work even when no text has been entered into the search field (as long as it is tapped to gain focus).