以编程方式更改 UITextField 键盘类型
是否可以以编程方式更改 uitextfield 的键盘类型,以便可以实现类似的操作:
if(user is prompted for numeric input only)
[textField setKeyboardType: @"Number Pad"];
if(user is prompted for alphanumeric input)
[textField setKeyboardType: @"Default"];
Is it possible to programmatically change the keyboard type of a uitextfield so that something like this would be possible:
if(user is prompted for numeric input only)
[textField setKeyboardType: @"Number Pad"];
if(user is prompted for alphanumeric input)
[textField setKeyboardType: @"Default"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
UITextField
有一个keyboardType
属性:您的代码应为
There is a
keyboardType
property for aUITextField
:Your code should read
值得注意的是,如果您希望当前焦点字段立即更新键盘类型,则需要执行一个额外步骤:
如果不调用
reloadInputViews
,键盘将不会更改,直到所选字段(第一响应者)失去并重新获得焦点。UIKeyboardType
值的完整列表 可以在此处找到,或者:It's worth noting that if you want a currently-focused field to update the keyboard type immediately, there's one extra step:
Without the call to
reloadInputViews
, the keyboard will not change until the selected field (the first responder) loses and regains focus.A full list of the
UIKeyboardType
values can be found here, or:是的,您可以,例如:
Yes you can, for example:
这是为了快速。另外,为了使其正常工作,必须在
textFieldView.delegate = self
之后设置它This is for swift. Also in order for this to function properly it must be set after the
textFieldView.delegate = self
要使文本字段接受字母数字,请仅设置此属性
to make the text field accept alpha numeric only set this property
Swift 4
如果您在满足条件时尝试更改键盘类型,请按照此操作。例如:如果我们想在文本字段的计数为 4 或 5 时将键盘类型从 默认 更改为 数字键盘,则执行以下操作:
Swift 4
If you are trying to change your keyboard type when a condition is met, follow this. For example: If we want to change the keyboard type from Default to Number Pad when the count of the textfield is 4 or 5, then do this:
有一个名为
keyboardType
的属性。您需要做的是将字符串
@"Number Pad
和@"Default
替换为UIKeyboardTypeNumberPad
和UIKeyboardTypeDefault< /代码>。
您的新代码应如下所示:
祝您好运!
There is a property for this called
keyboardType
.What you'll want to do is replace where you have strings
@"Number Pad
and@"Default
withUIKeyboardTypeNumberPad
andUIKeyboardTypeDefault
.Your new code should look something like this:
Good Luck!
对于想要使用 UIDatePicker 作为输入的人:
for people who want to use
UIDatePicker
as input:这是 Swift 3 的
UIKeyboardTypes
:这是使用列表中键盘类型的示例:
This is the
UIKeyboardTypes
for Swift 3:This is an example to use a keyboard type from the list:
以编程方式更改 UITextField 键盘类型 swift 3.0
Programmatically change UITextField Keyboard type swift 3.0
以下是 Swift 4.2 中的键盘类型
Here are the type of keyboard in Swift 4.2