(适用于 iOS 10 或更高版本)
Swift
在 Xcode 8.3/Swift 3.1 中,使用 .asciiCapableNumberPad
仅限于数字。
txtField.keyboardType = .asciiCapableNumberPad
Objective-C
txtField.keyboardType = UIKeyboardTypeASCIICapableNumberPad
@Jack的答案是正确的,但如果你需要支持iOS9及以下版本的iDevices,你需要像这样进行检查:
if (IS_OS_10_OR_LATER) {
txtfield.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
}
else {
txtfield.keyboardType = UIKeyboardTypeNumberPad;
}