Home > Software engineering >  TextField with autocorrectionDisabled() still shows predictive text bar
TextField with autocorrectionDisabled() still shows predictive text bar

Time:10-12

I experimenting with the SwiftUI TextField and want to get rid of the predictive suggestion bar above the keyboard:

struct ContentView: View {
    @State private var text: String = ""

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)

            TextField("Hello", text: $text)
                .autocorrectionDisabled()
                .textContentType(.init(rawValue: ""))
        }
        .padding()
    }
}

When typing, the predictive suggestions are still shown above the keyboard. Is there a way to get rid of these as well?

Screenshot

CodePudding user response:

You need to use autocorrectionDisabled instead of disableAutocorrection, add .textContentType(.init(rawValue: "")) and don't forget to set the keyboard to .keyboardType(.asciiCapable). Default keyboard doesn't work. I don't know why.

struct ContentView: View {
    @State private var text: String = ""
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            TextField("Hello", text: $text)
                .keyboardType(.asciiCapable)
                .autocorrectionDisabled(true)
                .textContentType(.init(rawValue: ""))
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  • Related