How to change UITextField keyboard type to email in Swift
Try this :
Swift 3
self.promoTextField.keyboardType = UIKeyboardType.emailAddress
// Or Shorter version
self.promoTextField.keyboardType = .emailAddress
Swift < 3
self.promoTextField.keyboardType = UIKeyboardType.EmailAddress
Programmatically change UITextField Keyboard type
There is a keyboardType
property for a UITextField
:
typedef enum {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad, // A number pad including a decimal point
UIKeyboardTypeTwitter, // Optimized for entering Twitter messages (shows # and @)
UIKeyboardTypeWebSearch, // Optimized for URL and search term entry (shows space and .)
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
} UIKeyboardType;
Your code should read
if(user is prompted for numeric input only)
[textField setKeyboardType:UIKeyboardTypeNumberPad];
if(user is prompted for alphanumeric input)
[textField setKeyboardType:UIKeyboardTypeDefault];
Change keyboard type of a text field in Swift during runtime
Just call reloadInputViews()
on your UITextField
after you change keyboard's type
textField.keyboardType = .numberPad
textField.reloadInputViews()
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)
Change keyboard input type programmatically (Swift)
Here is a solution from https://www.tutorialspoint.com/how-to-restrict-uitextfield-to-take-only-numbers-in-swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tf: UITextField!
override func viewDidLoad() {
tf.delegate = self
super.viewDidLoad()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let x = string.rangeOfCharacter(from: NSCharacterSet.decimalDigits) {
return true
} else {
return false
}
}
}
swift how to suggest user email in uitextfield as autocomplete
Set textContentType to emailAddress
let textField = UITextField()
textField.textContentType = .emailAddress
Related Topics
Could Not Cast Value of Type Uiview to [Customview] Using Xib
How to Unpack Multiple Levels of Nested JSON in Firebase Database
Avaudioplayer.Play() Works But Avaudioplayernode.Play() Fails
iOS 13 Swiftui: App Crashes Upon Launch on Real Device
Generic Class with Class-Bound Constraint Cannot Be Parametrized by Class-Bound Protocol
Collection View Layout Bug When Selectitem (Swift 5)
Swift: Filter a Dictionary with Array as Value
Undefined Symbol Objc_Class_$ When Creating iOS Framework
Filtering Dictionary in Swift 4 Fails in Xcode, But Succeeds in Playground
How to Read Ansi Escape Code Response Value in Swift
Exc Bad Access After Coding Signing
How to Know When to Call a Closure with () or Without Parentheses in Handler:
Making Gradient Background in Swift - the Right Way
How Assistant Model Works in Swift
Resizing Large Resolution Images Producing 1000X1000 Pixels Size When Size Is Set to 500X500 Pixels