如何显示“完成” iOS 数字键盘上的按钮?
.numberPad
键盘类型上没有“完成”按钮。 当用户在文本字段中输入数字信息后,如何使数字键盘消失?
我可以使用默认键盘获得“完成”按钮,但用户必须切换到数字键才能输入数字。 有没有办法在数字键盘上显示“完成”按钮?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
另一个解决方案。 如果屏幕上有其他非数字键盘文本字段,则完美。
Another solution. Perfect if there are other non-number pad text fields on the screen.
以下是 Luda 对 Swift 的回答的改编:
在 UIViewController 子类的声明中放入
ViewDidLoad put:
并添加函数 hoopla 和 hoopla (随意选择其他名称,只需相应地更改 ViewDidLoad 中的选择器名称即可)
Here is an adaptation for Luda's answer for Swift:
In the declaration of your UIViewController subclass put
in ViewDidLoad put:
and the add the functions hoopla and hoopla (feel free to choose other names, just change the selector names in ViewDidLoad accordingly
使用扩展的 Swift 3 解决方案。 如果您的应用中有多个数字
UITextField
对象,则非常理想,因为它可以灵活地为每个UITextField
决定是否在完成时执行自定义操作。点击 em> 或取消。使用默认操作的使用示例:
使用自定义 Done 操作的使用示例:
A Swift 3 solution using an extension. Ideal if you have several numeric
UITextField
objects in your app as it gives the flexibility to decide, for eachUITextField
, whether to perform a custom action when Done or Cancel is tapped.Example of usage using the default actions:
Example of usage using a custom Done action:
我见过的技巧是制作一个自定义透明按钮,其大小与整个视图相同,然后在其单击方法中,让文本字段放弃第一响应者。 因此,用户可以单击该字段之外的任何位置来关闭键盘。
The trick I've seen used is to make a custom transparent button the size of the whole view and then in its click method, have the text field resign first responder. So the user can click anywhere outside of the field to dismiss the keypad.
下面是对 Luda 答案的彻底修改,进行了以下更改:
附件视图的大小自动调整为应用程序框架的宽度
已弃用避免使用常量
UIBarButtonItemStyleBordered
“完成”按钮实例化为
UIBarButtonSystemItemDone
目前,“完成”按钮位于附件视图的中心。 您可以通过删除相关侧的空格将其放置在左侧或右侧。
我省略了“取消”按钮,因为默认键盘也没有该按钮。 如果您确实想要一个“取消”按钮,我建议您将其实例化为 UIBarButtonSystemItemCancel 并确保不会丢弃文本字段中的原始值。 Luda 的答案中实现的“取消”行为(用空白字符串覆盖该值)可能不是您想要的。
有关构建附件视图的更多信息,请参阅 Apple 文档 数据输入的自定义视图。 您可能需要查阅 UIToolbar 上的参考页以及 UIBarButtonItem 。
Below is an overhaul of Luda's answer with the following changes:
the accessory view is automatically sized to the width of the application frame
the deprecated constant
UIBarButtonItemStyleBordered
is avoidedthe "Done" button is instantiated as a
UIBarButtonSystemItemDone
Currently the "Done" button is centered in the accessory view. You can position it at left or right by deleting the space on the pertinent side.
I have omitted a "Cancel" button because the default keyboard doesn't have one either. If you do want a "Cancel" button, I suggest that you instantiate it as a
UIBarButtonSystemItemCancel
and that you make sure you're not discarding the original value in your text field. The "Cancel" behavior implemented in Luda's answer, which overwrites the value with a blank string, may not be what you want.For more information about building accessory views, see the Apple documentation on custom views for data input. You will probably want to consult the reference pages on UIToolbar and UIBarButtonItem as well.
UIKeyboardTypeNumberPad 和缺少返回键<中的解决方案/a> 效果很好,但前提是屏幕上没有其他非数字键盘文本字段。
我获取了该代码并将其转换为 UIViewController,您可以简单地对其进行子类化以使数字键盘正常工作。 您需要从上面的链接获取图标。
NumberPadViewController.h:
和 NumberPadViewController.m:
享受吧。
The solution in UIKeyboardTypeNumberPad and missing return key works great but only if there are no other non-number pad text fields on the screen.
I took that code and turned it into an UIViewController that you can simply subclass to make number pads work. You will need to get the icons from the above link.
NumberPadViewController.h:
and NumberPadViewController.m:
Enjoy.
如果您有多个数字字段,我建议对 UITextField 进行子类化以创建一个始终显示带有完成按钮的数字键盘的 NumericTextField。 然后,只需将您的数字字段与 Interface Builder 中的此类相关联,您就不需要在任何视图控制器中添加任何额外代码。 以下是我在 Xcode 8.0 中使用的 Swift 3.0 类。
斯威夫特4.2
If you have multiple numeric fields, I suggest subclassing UITextField to create a NumericTextField that always displays a numeric keyboard with a done button. Then, simply associate your numeric fields with this class in the Interface Builder and you won't need any additional code in any of your View Controllers. The following is Swift 3.0 class that I'm using in Xcode 8.0.
Swift 4.2
我发现 @user1258240 的答案非常简洁,因为这并不像设置
returnKeyType
那么简单财产。只是想为此贡献我自己的“可重用”方法:
I found @user1258240's answer to be pretty concise given this is not as simple as setting a
returnKeyType
property.Just wanted to contribute my own "re-usable" approach to this:
这是最新的代码。 只需包括
#import "UIViewController+NumPadReturn.h" 在你的 viewController 中。
这是 .h
和 .m
Here is the most recent code. Simply include
#import "UIViewController+NumPadReturn.h" in your viewController.
Here is the .h
And the .m
一个更简单的解决方案
A much easier solution
SWIFT 3.0 一种不同的风格,使用了之前一些答案的部分内容。
SWIFT 3.0 A different flavor, using parts of some previous answers.
我描述了一种适用于 iOS 4.2+ 的解决方案 此处,但关闭按钮会在键盘出现后淡入。 这并不可怕,但也不理想。
上面链接的问题中描述的解决方案包括一个更优雅的幻觉来关闭按钮,我在其中淡出并垂直移动按钮以提供键盘和按钮一起关闭的外观。
I describe one solution for iOS 4.2+ here but the dismiss button fades in after the keyboard appears. It's not terrible, but not ideal either.
The solution described in the question linked above includes a more elegant illusion to dismiss the button, where I fade and vertically displace the button to provide the appearance that the keypad and the button are dismissing together.
最简单方法是:
创建自定义透明按钮并将其放置在左下角,其
CGSize
与UIKeyboardTypeNumberPad
中的空白区域。 分别在单击按钮时在文本字段becomeFirstResponder
上切换(显示/隐藏)此按钮。The simplest way is:
Create custom transparent button and place it in left down corner, which will have same
CGSize
as empty space inUIKeyboardTypeNumberPad
. Toggle (show / hide) this button on textFieldbecomeFirstResponder
, on button click respectively.这是我遇到的最简单的解决方案。 我是从《Beginning iOS 5 Development》一书中学到这一点的。
假设数字字段名为
numberField
。在
ViewController
中,添加以下方法:在
ViewController.m
中,添加以下代码:Go back to
nib
file.Utilities
面板。Utilities
面板下的Identity Inspector
。View
(在 nib 文件中)一次。 确保您没有单击视图中的任何项目。 为了清楚起见,您应该在Identity Inspector
中的Class
下看到 UIView。连接检查器
。Touch Down
,然后将箭头放在File Owner
图标上。 (仅供参考...文件所有者图标显示在View
的左侧,并显示为带黄色框的空心立方体。)closeKeyboard
。现在,当您单击
View
背景上的任意位置时,您应该能够关闭键盘。希望这可以帮助您解决您的问题。 :-)
Here's the simplest solution I have come across. I have learnt this from Beginning iOS 5 Development book.
Assuming the number field is called
numberField
.In
ViewController
, add the following method:In
ViewController.m
, add the following code:Go back to
nib
file.Utilities
pan.Identity inspector
underUtilities
pan.View
(in nib file) once. Make sure you have not clicked on any of the items in the view. For the sake of clarification, you should see UIView underClass
inIdentity inspector
.Connection Inspector
.Touch Down
and drop the arrow onFile Owner
icon. (FYI... File Owner icon is displayed on the left ofView
and appears as a hollow cube with yellow frame.)closeKeyboard
.Now when you click anywhere on background of
View
, you should be able to dismiss the keyboard.Hope this helps you solve your problem. :-)
我将 Bryan 的解决方案修改得更加健壮,这样它就可以与可能出现在同一视图中的其他类型的键盘很好地配合。 此处描述:
在 iOS 数字键盘 UIKeyboard 上创建一个“完成”按钮
我想在这里解释一下,但其中大部分是要查看的代码,不太适合这里
I modified Bryan's solution to be a little more robust, so that it would play nicely with other types of keyboards that could appear in the same view. It's described here:
Create a DONE button on the iOS numpad UIKeyboard
I'd try to explain it here, but most of it is code to look at that wouldn't easily fit here
如果您事先知道要输入的数字数量(例如 4 位 PIN 码),则可以在按 4 次按键后自动关闭,按照我对此类似问题的回答:
关闭数字键盘
在这种情况下不需要额外的完成按钮。
If you know in advance the number of numbers to be entered (e.g. a 4-digit PIN) you could auto-dismiss after 4 key presses, as per my answer to this similar question:
dismissing Number Pad
No need for an additional done button in this case.
如果我们只是告诉视图控制器的视图结束编辑,我们还可以使“用户触摸其他地方”解决方案变得更加简单:
...假设“触摸其他地方会消除键盘”也是视图上任何其他可编辑字段的所需行为。
We can also make the "user touched somewhere else" solution even simpler if we just tell our view controller's view to end editing:
... assuming that "touching elsewhere dismisses the keyboard" is desired behavior for any other editable fields on the view as well.
对于 Swift 2.2 我用这个
For Swift 2.2 I use this
所有关于查找键盘视图并在第 3 行添加完成按钮(这就是为什么 button.y = 163 b/c 键盘高度为 216)的实现都很脆弱,因为 iOS 不断更改视图层次结构。 例如,以上代码均不适用于 iOS9。
我认为更安全的做法是通过 [[[UIApplication sharedApplication] windows] lastObject] 找到最上面的视图,然后在其左下角添加按钮,doneButton.frame = CGRectMake(0, SCREEN_HEIGHT-53, 106 , 53);//纵向模式
All those implementation about finding the keyboard view and adding the done button at the 3rd row (that is why button.y = 163 b/c keyboard's height is 216) are fragile because iOS keeps change the view hierarchy. For example none of above codes work for iOS9.
I think it is more safe to just find the topmost view, by [[[UIApplication sharedApplication] windows] lastObject], and just add the button at bottom left corner of it, doneButton.frame = CGRectMake(0, SCREEN_HEIGHT-53, 106, 53);// portrait mode
Swift 2.2 /我使用了Dx_的答案。 但是,我希望所有键盘上都有此功能。 因此,在我的基类中,我放置了以下代码:
然后只需在 viewDidLoad 中的 self.view.subviews 上调用 addDoneButtonForTextFields(如果使用表视图,则调用 willDisplayCell)即可将“完成”按钮添加到所有键盘。
Swift 2.2 / I used Dx_'s answer. However, I wanted this functionality on all keyboards. So in my base class I put the code:
Then just call addDoneButtonForTextFields on self.view.subviews in viewDidLoad (or willDisplayCell if using a table view) to add the Done button to all keyboards.