如何将 iOS 键盘限制为数字输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7409990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to limit iOS keyboard to numeric input
提问by Hyman BeNimble
The question pretty much says it all. Is there any way to have the iOS/iPhone keyboard default to numeric input?
这个问题几乎说明了一切。有没有办法让 iOS/iPhone 键盘默认为数字输入?
回答by Ravi
Set the keyboardType
property of the UITextField
to UIKeyboardTypeDecimalPad
.
将 的keyboardType
属性设置UITextField
为UIKeyboardTypeDecimalPad
。
Swift 3
斯威夫特 3
yourUITextField.keyboardType = .decimalPad
yourUITextField.keyboardType = .decimalPad
回答by Vette
Objective-C:
目标-C:
Default to Numbers (but include - and . if you need to allow decimal or negative numbers):
默认为数字(但包括 - 和 . 如果您需要允许小数或负数):
myTextField1.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
Default to Numbers (#s only for integer values)
默认为数字(#s 仅适用于整数值)
myTextField1.keyboardType=UIKeyboardTypeNumberPad;