如何在Swift中将UITextField键盘类型更改为电子邮件

时间:2014-10-16 12:24:53

标签: swift uitextfield

在objective-c中,我可以说以下内容。

[self.promoTextField setKeyboardType:UIKeyboardTypeEmailAddress];

我试过谷歌搜索它,但只是想方设法在objective-c中做到这一点。

7 个答案:

答案 0 :(得分:116)

试试这个:

Swift 3

self.promoTextField.keyboardType = UIKeyboardType.emailAddress

Swift< 3

self.promoTextField.keyboardType = UIKeyboardType.EmailAddress

答案 1 :(得分:14)

The documentation about UITextInputTraits, UITextField采用的协议,表示它仍在此处:

optional var keyboardType: UIKeyboardType { get set }

所有keyboardType的列表都是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
}

答案 2 :(得分:1)

Swift 3中,您可以使用:

youremailfieldname.keyboardType = UIKeyboardType.emailAddress

请注意lowercase

中的emailAddress
  

注意:您也可以在TextField中指定键盘类型   故事板中的定义。

enter image description here

答案 3 :(得分:0)

在Swift 3中尝试:

let emailTextField: UITextField = {
    let text = UITextField()
    text.keyboardType = .emailAddress

    return text
}()

答案 4 :(得分:0)

您应该只将条目类型设置为要更改的TextField。 例如

self.yourlTextField.keyboardType = .emailAddress //shows keyboard with e-mail characters

self.yourTextField.keyboardType = .phonePad ////shows phone pad only..just numbers

答案 5 :(得分:0)

有时给UITextField扩展添加一个方法,可以使用类似“ textField.chooseKeyboardType(keyboardType:.pad)”的方法,也许这不是一个好方法,但很有用。

private RemoteAPI api;
...
api = new RemoteAPI();

答案 6 :(得分:0)

不知道为什么,但为了获得最佳体验,我还必须设置一些其他属性。

TextField("Email", text: $email)
    .keyboardType(.emailAddress)
    .autocapitalization(UITextAutocapitalizationType.none)
    .disableAutocorrection(true)