禁用 UITextField 键盘?
我在应用程序中放置了一个数字键盘,用于将数字输入到文本视图中,但为了输入数字,我必须单击文本视图。一旦我这样做,常规键盘就会出现,这是我不想要的。
如何完全禁用键盘?非常感谢任何帮助。
I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard comes up, which I don't want.
How can I disable the keyboard altogether? Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
UITextField 的 inputView 属性默认为 nil,这意味着显示标准键盘。
如果您为其分配自定义输入视图,或只是虚拟视图,则键盘将不会出现,但闪烁的光标仍会出现:
如果您想隐藏键盘和闪烁的光标,请使用以下方法:
The UITextField's inputView property is nil by default, which means the standard keyboard gets displayed.
If you assign it a custom input view, or just a dummy view then the keyboard will not appear, but the blinking cursor will still appear:
If you want to hide both the keyboard and the blinking cursor then use this approach:
对于 Swift 2.x、3.x、4.x、5.x
可以解决这个问题
For Swift 2.x, 3.x, 4.x, 5.x
does the trick
如果它是 UITextField,您可以将其
enabled
属性设置为 NO。如果它是 UITextView,您可以在其委托中实现
-textViewShouldBeginEditing:
以返回 NO,这样它就永远不会开始编辑。或者您可以将其子类化并重写-canBecomeFirstResponder
以返回 NO。或者,您可以利用其编辑行为,将数字按钮放入用作文本视图的inputView
的视图中。这应该会导致在编辑文本视图时显示按钮。这可能是也可能不是您想要的。If it's a UITextField, you can set it's
enabled
property to NO.If it's a UITextView, you can implement
-textViewShouldBeginEditing:
in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override-canBecomeFirstResponder
to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view'sinputView
. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.根据您现有按钮的工作方式,这可能会破坏它们,但您可以阻止键盘显示,将 textView 的可编辑属性设置为 NO
Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO
当同一视图上有两个文本字段时,我遇到了同样的问题。我的目的是为一个文本字段显示默认键盘,然后隐藏第二个文本字段,然后显示一个下拉列表。
方法根本没有像我预期的那样工作 2 个文本字段,我发现的唯一解决方法是
这将完全隐藏目标文本字段的键盘(在我的情况下为 DropDownList)并在用户切换到时显示默认键盘第二个文本字段(屏幕截图上的帐号)
I have the same problem when had 2 textfields on the same view. My purpose was to show a default keyboard for one textfield and hide for second and show instead a dropdown list.
method simply did not work as I expected for 2 textfields , the only workaround I found was
This will totally hide the keyboard for a target textField (DropDownList in my case) and show a default one when user switches to the 2nd textfield (Account number on my screenshot)
有一个简单的技巧。放置一个空按钮
(无文本)位于键盘上方,并为其分配一个操作事件。这将停止键盘出现,您可以在按钮单击的手柄中执行任何您想要的操作
There is a simple hack to it. Place a empty button
(No Text) above the keyboard and have a action Event assign to it. This will stop keyboard coming up and you can perform any action you want in the handle for the button click
要禁用 UITextField 键盘:
要禁用 UITextView 键盘:
To disable UITextField keyboard:
To disable UITextView keyboard:
我使用了keyboardWillShow通知和textField.endEditing(true):
I used the
keyboardWillShow Notification
andtextField.endEditing(true)
:在 C# 中,这对我有用,我不使用故事板。
In C# this worked for me, I don't use the storyboard.
在 Xcode 8.2 中,您可以通过取消选中状态“启用”选项来轻松完成此操作。
或者如果您想通过代码来完成此操作。您可以简单地为此文本字段创建一个 @IBOutlet,为其指定一个名称,然后在 viewDidLoad 函数(或任何自定义函数,如果您愿意的话)中使用此变量名称,如下所示(在 swift 3.0.1 中):
In Xcode 8.2 you can do it easily by unchecking state "enabled" option.
Or if you want to do it via code. You can simply create an @IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):