18

I would like to turn off predictive text/autocorrect for a TextField in SwiftUI. Looks like this was possible in with UITextField: Disable UITextField Predictive Text

I checked the Apple documentation for TextField and googled, but can't find anything about this.

Has anyone found a way to disable the predictive text/autocomplete for a TextField?

Thank you!

dcvonb
  • 315
  • 3
  • 10

5 Answers5

27

Seems like it is now possible using Xcode 11 Beta 5. There is a new modifier to disable the autocorrection on TextField

func disableAutocorrection(_ disable: Bool?) -> some View

https://developer.apple.com/documentation/swiftui/textfield/3367734-disableautocorrection?changes=latest_beta

Edit: Modifier disableAutocorrection is deprecated in iOS 16.1. The new modifier is autocorrectionDisabled:

func autocorrectionDisabled(_ disable: Bool = true) -> some View

https://developer.apple.com/documentation/swiftui/presentedwindowcontent/autocorrectiondisabled(_:)?changes=latest_beta&language=_5

Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
szemian
  • 2,381
  • 3
  • 18
  • 22
8

This should work:

.disableAutocorrection(true)
DuDa
  • 3,718
  • 4
  • 16
  • 36
Eren Çelik
  • 99
  • 1
  • 1
4

Turns out you need to set the keyboard type to .alphabet for .disableAutocorrection(true) to work.

here is the extension I use:

extension View {
    func removePredictiveSuggestions() -> some View {
        self.keyboardType(.alphabet)
            .disableAutocorrection(true)
    }
}
Rom4in
  • 532
  • 4
  • 13
3

For the iOS 16.2 SDK, I needed to do this:

.keyboardType(.alphabet)
.textContentType(.oneTimeCode)
.autocorrectionDisabled(true)

(oneTimePasscode is an old UIKit hack to achieve the same result. It feels dirty to me, and I wouldn't be surprised if this behaviour changes again in the future...)

Matthew
  • 1,363
  • 11
  • 21
1

Xcode 12.3 Swift 5.3

If you need to disable autocorrection on multiple TextFields, or indeed add other modifiers, then create a custom TextField:

struct TextFieldCustom: View {
    
    let title: String
    let text: Binding<String>
    
    init(_ title: String, text: Binding<String>) {
        self.title = title
        self.text = text
    }
    
    var body: some View {
        TextField(title, text: text)
            .disableAutocorrection(true)
            // add any other modifiers that you want
    }
}

Example Usage:

Form {
    Section(header: Text("Details")) {
        TextFieldCustom("Field1", text: $field1)
        TextFieldCustom("Feild2", text: $field2)
        TextFieldCustom("Field3", text: $field3)
    }
}
rbaldwin
  • 4,581
  • 27
  • 38