19

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 iOS 15 SwiftUI .searchable API.

Chris
  • 4,009
  • 3
  • 21
  • 52

2 Answers2

34

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)

For iOS 16+

From iOS 16 onwards disableAutocorrection is deprecated. So need to use autocorrectionDisabled instead.


List {
    //
}
.searchable(text: $searchText)
.autocorrectionDisabled(true) 
SamB
  • 1,560
  • 1
  • 13
  • 19
  • This works! I though I had tried it like this but clearly not. I though that modifier could only be used for `TextField`s. Thanks. – Chris Oct 21 '21 at 14:49
  • 1
    You are welcome and glad it solved your problem :) – SamB Oct 21 '21 at 15:24
6

disableAutocorrect will be deprecated in a future iOS update (current version: iOS 16.3). Use autocorrectionDisabled instead:

List {
    //
}
.searchable(text: $searchText)
.autocorrectionDisabled(true)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Adam
  • 153
  • 1
  • 8