How to set Keyboard type of TextField in SwiftUI?
To create a field where you can enter secure text you can use SecureField($password)
https://developer.apple.com/documentation/swiftui/securefield
If you want to set the contentType
of an textField to eg. .oneTimeCode
you can do it like this. Also works with keyboardType
TextField($code)
.textContentType(.oneTimeCode)
.keyboardType(.numberPad)
We no longer need to use hacks. Beta 5 has brought a new modifier to set the keyboard type. For example, to use a number pad:
.keyboardType(.numberPad)
There's also autocapitalization()
and an EnvironmentValue disableAutocorrection
.
To set TextField's
keyboard type you need to add modifier .keyboardType()
.
TextField(placeHolder, text: "your text here")
.keyboardType(.numberPad)
Set .keyboardType(.numberPad)
on the TextField. Simple!