如何添加“完成”使用SWIFT在iOS中进行numpad的按钮?
默认键盘可以正常工作,但是我无法与NumPad一起使用。
有什么想法吗?
It works fine with the default keyboard, but I cant get it working with the numpad.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
据我所知,您无法在键盘部分上添加完成按钮;您将
inputAccessoryView
添加到uitextfield
或uitextview
(如果您正在使用的话)。检查文档有关更多信息。
编辑:检查这个问题以示例如何做。
编辑2 :相似 swift中的示例。
编辑3 :编辑2中的代码,因为链接可能会过期。
Swift 4.2
As far as I know, you can't add the Done button on the keyboard part; you'd have add a
inputAccessoryView
to theUITextField
orUITextView
(if that's what you're using).Check the documentation for more info.
Edit: Check this question for an example on how to do that.
Edit 2: Similar example in Swift.
Edit 3: Code from edit 2, as link may expire.
Swift 4.2
Swift 5 使用
@ibinpectable
和扩展程序解决方案。项目中每个UITEXTFIELD的接口构建器中的属性检查器下的下拉列表将出现。Swift 5 solution using
@IBInpectable
and extension. A dropdown will appear under the Attribute Inspector in the Interface Builder for every UITextField in the project.Swift -3版本(Marko的解决方案的 - 对我有用)
(在我的情况下,我的Uitextfield被识别为
textfield
)A Swift-3 Version (of Marko's solution - which worked for me)
(in my case, I have a UITextField identified as
textfield
)您可以添加带有完成按钮的工具栏。
以下是我在情况下使用的代码
You can add toolbar with Done button to keyboard.
InputAccessoryView
property of textfield can be used to set this toolbar.Below is code that I’ve used in my case
我可以找到的最简单的解决方案 - 与 swift 4.2 一起使用 - 希望这会有所帮助:)
然后,您可以在
textFieldDidediting
委托方法中处理完成的操作,或者只需在扩展程序中添加自定义方法,然后将其设置在donebutton
的选择中。The simplest solution I could find - works with Swift 4.2 - hope this helps :)
Then you can handle the done action in the
textFieldDidEndEditing
delegate method or just add a custom method to the extension and set it in the selector of thedoneButton
.如果您的
完成
按钮应该仅关闭编号板,则最简单的版本是调用wisefirstresponder
作为选择器:此代码在ios9和swift 2中起作用,我在我的应用中使用它:
If your
Done
button is supposed to just close the numberpad, then the simplest version would be to callresignFirstResponder
as the selector like this:This code works in iOS9 and Swift 2, I am using it in my app:
首先,让我告诉您一件事,您可以在默认键盘上添加完成按钮。实际上,今天我只是解决了这个问题并解决了。现成的代码,只需将其放在您的.swift课程上即可。我正在使用Xcode 7并使用iPad Retina(iOS 9)对此进行测试。
无论如何,这里不再说话是代码。
这给了我的解决方案为...
我只想告诉你这是我在上使用的工作解决方案我最近的项目。我之所以发布此问题,是因为没有用于此问题的工作解决方案。工作代码和解释如所承诺的。
编辑: -
使其更专业...如果您遵循上述代码,它也将为您提供工作解决方案,但是在这里我试图使其更专业。请注意代码mod。我将以前未使用的代码留下了报价。
更改..
使用FlexSpace和negativespace将自定义按钮定位在屏幕左侧。
现在,它给了我以下内容...此外,尝试匹配图片并找到更改。
谢谢。
希望这有所帮助。对不起,回答很长。
First of all let me tell you one thing that You Can ADD A Done Button On Your Default KeyBoard. Actually today I just went through this problem and solved. Ready Made Code, just place this on your .swift class. I am using Xcode 7 and testing this using iPad Retina(ios 9).
Anyway no more talk here is the code.
Which is giving me the solution as...
One more thing I just wanna tell you that this is the working solution which I have used on my recent project. I have posted this because there is no working solution available for this problem. Working Code and explanation as promised.
EDIT :-
Making it more professional...If you are following the above code then also it will give you the working solution but here I am trying to make it more Professional. Please notice the code MOD. I left the previous unused code with quote.
Changes..
Positioning the custom button to the left of the screen using FlexSpace and NegativeSpace.
It is now giving me the following...Moreover try to match the pictures and find the changes.
Thanks.
Hope this helped. Sorry for the long answer.
我将使用库calles iqkeyboardmanagerswift 。
Simplay必须将一行代码插入AppDelegates DID FinishWithLaunching方法,并且它将所有必要的功能应用于所有Textfields。
I would use a library calles IQKeyboardManagerSwift.
You simplay have to insert one line of code into AppDelegates didFinishWithLaunching method and it applies all necessary features to all TextFields.
因为上述答案在Xcode 11中及以上是最初的工具栏,例如
uitoolbar()
每次都会像在这里您运行uitoolbar(框架:cgrect(x:0,y:0,width:view.frame.size.width,高度:35)))
替换示例代码
Because above answers are old in Xcode 11 and above initial toolBar like
UIToolbar()
will throw a breaking constraint on console every time like here you run so useUIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
insteadSample code
根据Marko Nikolovski的说法,
这里的答案是Objective-C答案:
According to Marko Nikolovski answer
Here is objective-C answer :
您可以创建一个自定义类。我正在使用 swift 4 :
You can create a custom class. I'm using Swift 4:
在任何键盘上添加“完成”按钮的简单简便方法是添加Universal Library
iqkeyboard manager
AppDelegate在应用程序(_应用程序:uiapplication,did finishlaunchingwithoptions。
simple and easy way to add "DONE" button on any keyboard is add universal library
IQKeyboardManager
add below code in appdelegate at application(_ application: UIApplication, didFinishLaunchingWithOptions.