Search code examples
iosswiftsearchswiftuiios15

Disable autocorrect on a SwiftUI searchable search bar


I have a SwiftUI and Core Data app and have implemented the new iOS 15 search bar API.

.searchable(text: $searchText) // This is a modifier under my List view

However, the search bar has autocorrect, which unexpectedly changes the search when the view disappears or the user commits (it even happens if navigating to a detail view and back). Overall it is a poor user experience.

I cannot find anything in the Apple documentation for disabling autocorrect on this search bar (although it is easily done for a standard TextField with the .disableAutocorrect(true) modifier).

I used a Swift Package for iOS14 that provided a search bar (via UIViewRepresentable), but I would rather use first party APIs if possible, so my question relates specifically to the SwiftUI .searchable API, which was introduced in iOS 15.


Solution

  • For iOS 16+

    For iOS 16 onwards need to use autocorrectionDisabled as below after .searchable(text: $searchText)

    
    List {
        //
    }
    .searchable(text: $searchText)
    .autocorrectionDisabled(true) 
    

    For iOS 15

    The auto correction of the search bar get disabled if you set disableAutocorrection(true) after .searchable(text: $searchText).

    List {
        //
    }
    .searchable(text: $searchText)
    .disableAutocorrection(true)