TechnologiesSwiftUISearch and Find

FindContext struct

iOSmacOStvOSwatchOSvisionOS✓ renders

The status of the find navigator for views which support text editing.

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.

  1. Attach the find bar with findNavigator(isPresented:)

    FindContext describes a find-and-replace session that the find navigator brings into existence. You opt a text view into that experience by applying findNavigator(isPresented:) to it; in the example it modifies the TextEditor so the find bar can be shown over the editor's content. Without this modifier there is no find interaction for the context to represent.

  2. Bind the presentation state

    The isPresented binding is the toggle that opens and closes the find experience whose state FindContext carries. 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.

  3. 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") sets isPresented = true, programmatically entering the find-and-replace flow that FindContext governs instead of relying solely on the keyboard shortcut.

  4. Connect it to the searchable text

    FindContext operates 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 $text into TextEditor(text:), so matches found and replacements made through the find experience flow back into your own state.

Try it — Change 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.

FindContext.swift
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()
    }
}
Live preview
Search inside this text using the find bar. Find
Search inside this text using the find bar. Find
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →