Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: Is it possible to turn off predictive text for a TextField

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!

like image 490
dcvonb Avatar asked Jul 26 '19 05:07

dcvonb


3 Answers

This should work:

.disableAutocorrection(true)
like image 66
Eren Çelik Avatar answered Sep 21 '22 15:09

Eren Çelik


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

like image 27
szemian Avatar answered Sep 18 '22 15:09

szemian


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)
    }
}
like image 33
rbaldwin Avatar answered Sep 21 '22 15:09

rbaldwin