【问题标题】:How can I show the numeric keyboard by default on iPhone?如何在 iPhone 上默认显示数字键盘?
【发布时间】:2011-04-17 08:12:25
【问题描述】:

我正在尝试以下方法,但没有运气。当用户单击 UiTextfield 时,我需要将键盘视图自动更改为数字视图,这可能吗?

【问题讨论】:

    标签: iphone ios objective-c keyboard uitextfield


    【解决方案1】:

    UITextInputTraits 协议(UITextField 符合)具有 keyboardType 类型为 UIKeyboardType 的属性。

    myTextField.keyboardType = UIKeyboardTypeNumberPad;
    

    【讨论】:

    • 在 Swift 4 中:myTextField.keyboardType = .numberPad
    【解决方案2】:

    这也可以在 Interface Builder 中完成:

    【讨论】:

      【解决方案3】:

      UITextField 符合 UITextInputTraits 协议,这意味着它具有可以设置的keyboardType 属性。

      myField.keyboardType = UIKeyboardTypeNumberPad;

      您也可以通过界面生成器进行设置。从文本输入特征将键盘设置为数字键盘,仅此而已。

      【讨论】:

        【解决方案4】:

        我们也可以通过界面方式获取它。选择文本字段并转到属性检查器并从键盘下拉框中更改键盘类型。

        【讨论】:

          【解决方案5】:

          如果您想显示数字键盘,但又需要能够使用小数点、货币符号或其他字符,您可以将键盘默认设置为字母键盘的背面。

          currentTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
          

          【讨论】:

            【解决方案6】:

            最好始终编写父类UIKeyboardType,然后继续使用键盘枚举numberPad

            myTextField.keyboardType = UIKeyboardType.numberPad
            

            【讨论】:

            • 你不要在点之前have to write the parent class。事实上,建议不要在最新版本的 Swift 中编写它。只需从点开始,Xcode 就会正确地自动完成。
            猜你喜欢
            • 2010-12-23
            • 2010-09-20
            • 2011-10-08
            • 2011-09-08
            • 2016-12-19
            • 1970-01-01
            • 2015-02-09
            • 2010-10-02
            • 1970-01-01
            相关资源
            最近更新 更多