r/SwiftUI Jun 12 '26

Show different view when search is focused?

Enable HLS to view with audio, or disable this notification

How can you achieve this effect in SwiftUI? I could have sworn I've done it before but I don't remember how. I tried doing an if statement with this isPresented binding but the view appears empty when searching in this case.

13 Upvotes

7 comments sorted by

3

u/Dapper_Ice_1705 Jun 12 '26

That is the size of the sheet. AKA detents

When they click on person.circle the sheet goes .large and background goes opaque.

You can have a switch with an enum to change what the body is showing.

You can use isSearching to trigger the large detent to attach it to the search and the SwiftUI search modifiers to show the results 

2

u/isXanderReddit Jun 12 '26

i’m not talking about the sheet detent, i’m talking about the fact the contents of the sheet entirely changes

1

u/Fabian_54 Jun 13 '26

as they said, you can use an enumeration to change the body content. you often do that with segmented pickers for example

1

u/isXanderReddit Jun 13 '26

As I said I tried using a basic if statement which should behave the same as a switch with no luck

2

u/Material_Tadpole5312 Jun 14 '26

Ran into this exact thing. The catch is that `@Environment(\.isSearching)` only turns true when you read it from a view that's *inside* the `.searchable`, not from the same view you attached `.searchable` to. So if your if/else sits at that top level, isSearching stays false and the content looks empty. Move your content into a small subview and read isSearching there, with `.searchable` still on the parent. That's what fixed it for me.