How it works
FindContext is the value SwiftUI hands to your view to describe the state of an in-progress find-and-replace interaction, the kind a text view exposes through its find bar. Rather than tracking whether the find UI is showing or what the user is searching for yourself, you read or drive that state through this context, keeping your view in step with the system find experience. Reach for FindContext when you present text editing — such as a TextEditor — and want to surface, control, or respond to the find bar programmatically.
Attach the find bar with findNavigator(isPresented:)
FindContextdescribes a find-and-replace session that the find navigator brings into existence. You opt a text view into that experience by applyingfindNavigator(isPresented:)to it; in the example it modifies theTextEditorso the find bar can be shown over the editor's content. Without this modifier there is no find interaction for the context to represent.Bind the presentation state
The
isPresentedbinding is the toggle that opens and closes the find experience whose stateFindContextcarries. Here it is wired to@State private var isPresented, so flipping that value reveals or dismisses the find bar — letting you present find from anywhere in your interface rather than waiting on a built-in gesture.Drive find from your own controls
Because presentation is just a binding, you can begin a find session from ordinary SwiftUI controls. The example's
Button("Find")setsisPresented = true, programmatically entering the find-and-replace flow thatFindContextgoverns instead of relying solely on the keyboard shortcut.Connect it to the searchable text
FindContextoperates over the text the find bar searches and edits — the value bound into the editor. In the example that is@State private var text, projected as$textintoTextEditor(text:), so matches found and replacements made through the find experience flow back into your own state.
Button("Find") { isPresented = true } to toggle with isPresented.toggle() and tap it twice to watch the find bar dismiss, confirming the binding both opens and closes the find interaction.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct FindContextDemo: View {
@State private var text = "Search inside this text using the find bar."
@State private var isPresented = false
var body: some View {
VStack {
TextEditor(text: $text)
.frame(height: 120)
.border(.gray)
.findNavigator(isPresented: $isPresented)
Button("Find") { isPresented = true }
}
.padding()
}
}