【问题标题】:How to change UITextField keyboard type to email in Swift如何在 Swift 中将 UITextField 键盘类型更改为电子邮件
【发布时间】:2014-10-16 12:24:53
【问题描述】:

在objective-c中我只能说以下内容。

[self.promoTextField setKeyboardType:UIKeyboardTypeEmailAddress];

我尝试用谷歌搜索它,但只是在 Objective-c 中找到了方法。

【问题讨论】:

标签: swift uitextfield


【解决方案1】:

试试这个:

斯威夫特 3

self.promoTextField.keyboardType = UIKeyboardType.emailAddress
// Or Shorter version
self.promoTextField.keyboardType = .emailAddress

斯威夫特
self.promoTextField.keyboardType = UIKeyboardType.EmailAddress

【讨论】:

  • 你也可以这样使用速记:self.promoTextField.keyboardType = .EmailAddress
  • 如果需要更改当前焦点文本字段的keyboardType而不从第一响应者那里退出,则必须在更改keyboardType后调用reloadInputViews()。示例:self.promoTextField.reloadInputViews()
  • 有谁知道为什么第一次设置它有效,但对于以后的,你必须将.keyboardType更改为其他内容然后再返回?
【解决方案2】:

The documentation about UITextInputTraits,UITextField采用的协议,说它还在:

optional var keyboardType: UIKeyboardType { get set }

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

【讨论】:

【解决方案3】:

Swift 3 你可能会使用:

youremailfieldname.keyboardType = UIKeyboardType.emailAddress

注意emailAddress中的lowercase

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

【讨论】:

  • 这个答案对已经存在的答案没有任何新意。
  • 其他答案不包括情节提要选项,谢谢
【解决方案4】:

在 Swift 3 中尝试一下:

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

    return text
}()

【讨论】:

    【解决方案5】:

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

    self.yourlTextField.keyboardType = .emailAddress //shows keyboard with e-mail characters
    
    self.yourTextField.keyboardType = .phonePad ////shows phone pad only..just numbers
    

    【讨论】:

      【解决方案6】:

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

      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
          }
      }
      }
      

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

          猜你喜欢
          • 2012-04-29
          • 1970-01-01
          • 1970-01-01
          • 2021-03-20
          • 2017-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多