如何加上「;“完成”;使用Swift在iOS中使用Numpad的按钮?
它可以与默认键盘配合使用,但我无法让它与numpad配合使用如何加上「;“完成”;使用Swift在iOS中使用Numpad的按钮?,ios,swift,numpad,Ios,Swift,Numpad,它可以与默认键盘配合使用,但我无法让它与numpad配合使用 有什么想法吗?据我所知,您不能在键盘部分添加“完成”按钮;您需要在UITextField或UITextView中添加一个inputAccessoryView(如果您正在使用) 检查一下 编辑:检查如何进行编辑 编辑2:类似 编辑3:来自编辑2的代码,因为链接可能过期 override func viewDidLoad() { super.viewDidLoad() //--- add UIToolBar on key
有什么想法吗?据我所知,您不能在键盘部分添加“完成”按钮;您需要在
UITextField
或UITextView
中添加一个inputAccessoryView
(如果您正在使用)
检查一下
编辑:检查如何进行编辑
编辑2:类似
编辑3:来自编辑2的代码,因为链接可能过期
override func viewDidLoad()
{
super.viewDidLoad()
//--- add UIToolBar on keyboard and Done button on UIToolBar ---//
self.addDoneButtonOnKeyboard()
}
//--- *** ---//
func addDoneButtonOnKeyboard()
{
var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
doneToolbar.barStyle = UIBarStyle.BlackTranslucent
var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))
var items = NSMutableArray()
items.addObject(flexSpace)
items.addObject(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
self.textView.inputAccessoryView = doneToolbar
self.textField.inputAccessoryView = doneToolbar
}
func doneButtonAction()
{
self.textViewDescription.resignFirstResponder()
}
Swift 4.2
func addDoneButtonOnKeyboard(){
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
doneToolbar.barStyle = .default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
let items = [flexSpace, done]
doneToolbar.items = items
doneToolbar.sizeToFit()
txtMobileNumber.inputAccessoryView = doneToolbar
}
@objc func doneButtonAction(){
txtMobileNumber.resignFirstResponder()
}
首先,让我告诉您一件事,您可以在默认键盘上添加“完成”按钮。事实上,今天我刚刚解决了这个问题。准备好的代码,把它放在你的.swift类上。我正在使用Xcode 7,并使用iPadRetina(iOS9)进行测试 不管怎样,这里没有更多的谈话,这是代码
//Creating an outlet for the textfield
@IBOutlet weak var outletTextFieldTime: UITextField!
//Adding the protocol of UITextFieldDelegate
class ReservationClass: UIViewController, UITextFieldDelegate {
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
//Making A toolbar
let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/17))
//Setting the style for the toolbar
keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent
//Making the done button and calling the textFieldShouldReturn native method for hidding the keyboard.
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:"))
//Calculating the flexible Space.
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
//Setting the color of the button.
item.tintColor = UIColor .yellowColor()
//Making an object using the button and space for the toolbar
let toolbarButton = [flexSpace,doneButton]
//Adding the object for toolbar to the toolbar itself
keyboardDoneButtonShow.setItems(toolbarButton, animated: false)
//Now adding the complete thing against the desired textfield
outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow
return true
}
//Function for hidding the keyboard.
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}
这给了我一个解决方案
还有一件事我只想告诉你,这是我在最近的项目中使用的工作解决方案。我之所以发布这篇文章,是因为这个问题没有有效的解决方案。工作代码和解释符合承诺
编辑:-
让它更专业…如果你遵循上面的代码,那么它也会给你工作的解决方案,但在这里,我试图使它更专业。请注意代码MOD。我将以前未使用的代码保留在quote中
变化
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(200,200, self.view.frame.size.width,30))
// self.view.frame.size.height/17
keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent
let button: UIButton = UIButton()
button.frame = CGRectMake(0, 0, 65, 20)
button.setTitle("Done", forState: UIControlState .Normal)
button.addTarget(self, action: Selector("textFieldShouldReturn:"), forControlEvents: UIControlEvents .TouchUpInside)
button.backgroundColor = UIColor .clearColor()
// let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:"))
let doneButton: UIBarButtonItem = UIBarButtonItem()
doneButton.customView = button
let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpace.width = -10.0
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
//doneButton.tintColor = UIColor .yellowColor()
let toolbarButton = [flexSpace,doneButton,negativeSpace]
keyboardDoneButtonShow.setItems(toolbarButton, animated: false)
outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow
return true
}
希望这有帮助。很抱歉回答得太长。您可以将带有“完成”按钮的工具栏添加到键盘。
文本字段的InputAccessoryView
属性可用于设置此工具栏
下面是我在案例中使用的代码
//Add done button to numeric pad keyboard
let toolbarDone = UIToolbar.init()
toolbarDone.sizeToFit()
let barBtnDone = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done,
target: self, action: #selector(VerifyCardViewController.doneButton_Clicked(_:)))
toolbarDone.items = [barBtnDone] // You can even add cancel button too
txtCardDetails3.inputAccessoryView = toolbarDone
如果您的
Done
按钮应该只是关闭数字广告,那么最简单的版本就是调用resignFirstResponder
作为选择器,如下所示:
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder))
此代码适用于iOS9和Swift 2,我正在我的应用程序中使用它:
func addDoneButtonOnNumpad(textField: UITextField) {
let keypadToolbar: UIToolbar = UIToolbar()
// add a done button to the numberpad
keypadToolbar.items=[
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
]
keypadToolbar.sizeToFit()
// add a toolbar with a done button above the number pad
textField.inputAccessoryView = keypadToolbar
}
一个Swift-3版本(Marko的解决方案的版本-对我有效) (在我的例子中,我有一个UITextField,标识为
textfield
)根据Marko Nikolovski的回答 以下是客观C答案:
- (void)ViewDidLoad {
[self addDoneButtonOnKeyboard];
}
- (void)addDoneButtonOnKeyboard {
UIToolbar *toolBarbutton = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBarbutton.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonAction)];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barBtnItem];
[items addObject:done];
toolBarbutton.items = items;
[toolBarbutton sizeToFit];
self.timeoutTextField.inputAccessoryView = toolBarbutton;
}
- (void)doneButtonAction {
[self.view endEditing:YES];
}
Swift 5使用
@ibInputable
和扩展的解决方案。对于项目中的每个UITextField,Interface Builder中的属性检查器下将显示一个下拉列表
extension UITextField{
@IBInspectable var doneAccessory: Bool{
get{
return self.doneAccessory
}
set (hasDone) {
if hasDone{
addDoneButtonOnKeyboard()
}
}
}
func addDoneButtonOnKeyboard()
{
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
doneToolbar.barStyle = .default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
let items = [flexSpace, done]
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
@objc func doneButtonAction()
{
self.resignFirstResponder()
}
}
您可以创建自定义类。我正在使用Swift 4:
class UITextFieldWithDoneButton: UITextField {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addDoneButtonOnKeyboard()
}
fileprivate func addDoneButtonOnKeyboard() {
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
doneToolbar.barStyle = .default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
let items = [flexSpace, done]
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
@objc fileprivate func doneButtonAction() {
self.resignFirstResponder()
}
}
我能找到的最简单的解决方案-适用于Swift 4.2-希望这有帮助:) 扩展UITextField{ func addDoneButtonOnKeyboard(){ 让keyboardToolbar=UIToolbar() keyboardToolbar.sizeToFit() 设flexibleSpace=UIBarButtonItem(barButtonSystemItem:.flexibleSpace, 目标:无,行动:无) 让doneButton=UIBarButtonItem(barButtonSystemItem:.done, 目标:自我,行动:#选择器(辞职第一响应者)) keyboardToolbar.items=[flexibleSpace,doneButton] self.inputAccessoryView=键盘工具栏 } }
然后,您可以在
textfielddendediting
委托方法中处理完成的操作,或者只需向扩展添加一个自定义方法,并在doneButton
的选择器中设置它在任何键盘上添加“完成”按钮的简单方法是添加通用库
在应用程序的appdelegate中添加以下代码(application:UIApplication,didFinishLaunchingWithOptions)
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.enableAutoToolbar = true
IQKeyboardManager.shared.keyboardDistanceFromTextField = 15
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
我会用一个图书馆来打电话 您只需在AppDelegates didFinishWithLaunching方法中插入一行代码,它将所有必要的功能应用于所有文本字段
import IQKeyboardManagerSwift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Following line is all you need and it applies for all TextFields
IQKeyboardManager.shared.enable = true
return true
}
因为上面的答案在Xcode 11及以上版本中很旧,所以像
UIToolbar()
这样的初始工具栏每次运行时都会在控制台上抛出一个中断约束,所以请使用
UIToolbar(frame:CGRect(x:0,y:0,width:view.frame.size.width,height:35))
示例代码
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
toolBar.barStyle = .default
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneButtonTapped))
toolBar.items = [doneButton]
toolBar.isUserInteractionEnabled = true
textField.inputAccessoryView = toolBar
查看此链接我对创建新键盘不感兴趣,我只想在UIToolBar中添加一个按钮,然后将其作为inputAccessoryView添加到文本字段,如下图所示:,但我还没有找到它,如何在swift中实现。我们可以通过在默认键盘中添加自定义按钮来代替自定义键盘或工具栏。请参阅他使用故事板的.For解决方案非常有效。实现起来既快又简单。谢谢!您可以更改此选项,并且不必本地化按钮:let done:UIBarButtonItem=UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.done,target:self,action:#选择器(self.doneButtonAction))回答太棒了。我希望每个人都在这里滚动。这正是我想要的。感谢简洁、简单、优雅的解决方案!这是可行的,但你是如何设计出35帧的?我认为它是默认高度,可能是你可以配置它来更改你可以设置任何数字(尝试0到999)。这似乎没有改变任何东西。这对我来说很有用。只需复制并粘贴到.swift文件的底部,然后将:textfieldOutput.addDoneButtonOnKeyboard()用于ViewDidLoad。谢谢!
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
toolBar.barStyle = .default
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneButtonTapped))
toolBar.items = [doneButton]
toolBar.isUserInteractionEnabled = true
textField.inputAccessoryView = toolBar