How to change UITextField keyboard type to email in Swift

后端 未结 6 1084
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 20:36

In objective-c I can just say the following.

[self.promoTextField setKeyboardType:UIKeyboardTypeEmailAddress];

I tried googling it but just ge

相关标签:
6条回答
  • 2021-01-30 20:50

    You should just set the type of entry to TextField you want to change. e.g.

    self.yourlTextField.keyboardType = .emailAddress //shows keyboard with e-mail characters
    
    self.yourTextField.keyboardType = .phonePad ////shows phone pad only..just numbers
    
    0 讨论(0)
  • 2021-01-30 20:53

    Try it in Swift 3:

    let emailTextField: UITextField = {
        let text = UITextField()
        text.keyboardType = .emailAddress
    
        return text
    }()
    
    0 讨论(0)
  • 2021-01-30 20:54

    Try this :

    Swift 3

    self.promoTextField.keyboardType = UIKeyboardType.emailAddress
    

    Swift < 3

    self.promoTextField.keyboardType = UIKeyboardType.EmailAddress
    
    0 讨论(0)
  • 2021-01-30 20:57

    In Swift 3 you might use:

    youremailfieldname.keyboardType = UIKeyboardType.emailAddress

    Note the lowercase in emailAddress

    Note: also that you can assign the keyboard type in the TextField definition in the storyboard.

    0 讨论(0)
  • 2021-01-30 20:59

    The documentation about UITextInputTraits, a protocol adopted by UITextField, says it's still here:

    optional var keyboardType: UIKeyboardType { get set }
    

    And the list of all keyboardTypes is here :

    enum UIKeyboardType : Int {
        case Default
        case ASCIICapable
        case NumbersAndPunctuation
        case URL
        case NumberPad
        case PhonePad
        case NamePhonePad
        case EmailAddress
        case DecimalPad
        case Twitter
        case WebSearch
    }
    
    0 讨论(0)
  • 2021-01-30 21:02

    Sometimes give UITextField extension to add one method, you can use like that "textField.chooseKeyboardType(keyboardType:.pad)", perhaps this is not a good way, but useful.

    public extension UITextField {
    
    func chooseKeyboardType(_ keyboardType:UIKeyboardType) {
        switch keyboardType {
        case .emailAddress:
            self.keyboardType = .emailAddress
        case .twitter:
            self.keyboardType = .twitter
        case .numberPad:
            self.keyboardType = .numberPad
        case .numbersAndPunctuation:
            self.keyboardType = .numbersAndPunctuation
        case .asciiCapable:
            self.keyboardType = .asciiCapable
        case .URL:
            self.keyboardType = .URL
        case .decimalPad:
            self.keyboardType = .decimalPad
        case .namePhonePad:
            self.keyboardType = .namePhonePad
    
    
        default:
            self.keyboardType = .default
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题