在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
当前回答
使用扩展的Swift 3解决方案。如果你在你的应用程序中有几个数值的UITextField对象,这是理想的,因为它可以灵活地决定,对于每个UITextField,当点击完成或取消时是否执行自定义操作。
//
// UITextField+DoneCancelToolbar.swift
//
import UIKit
extension UITextField {
func addDoneCancelToolbar(onDone: (target: Any, action: Selector)? = nil, onCancel: (target: Any, action: Selector)? = nil) {
let onCancel = onCancel ?? (target: self, action: #selector(cancelButtonTapped))
let onDone = onDone ?? (target: self, action: #selector(doneButtonTapped))
let toolbar: UIToolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: onCancel.target, action: onCancel.action),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Done", style: .done, target: onDone.target, action: onDone.action)
]
toolbar.sizeToFit()
self.inputAccessoryView = toolbar
}
// Default actions:
func doneButtonTapped() { self.resignFirstResponder() }
func cancelButtonTapped() { self.resignFirstResponder() }
}
使用默认动作的示例:
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet { myNumericTextField?.addDoneCancelToolbar() }
}
使用自定义完成动作的示例:
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet {
myNumericTextField?.addDoneCancelToolbar(onDone: (target: self, action: #selector(doneButtonTappedForMyNumericTextField)))
}
}
func doneButtonTappedForMyNumericTextField() {
print("Done");
myNumericTextField.resignFirstResponder()
}
其他回答
以下是对Luda的回答的全面检查,并进行了以下更改:
附件视图会自动调整到应用程序框架的宽度 避免使用已弃用的常量UIBarButtonItemStyleBordered Done按钮被实例化为UIBarButtonSystemItemDone
目前“Done”按钮位于附件视图的中央。您可以通过删除相关一侧的空格将其定位在左侧或右侧。
我省略了“取消”按钮,因为默认键盘也没有这个按钮。如果你确实想要一个“取消”按钮,我建议你实例化它为一个UIBarButtonSystemItemCancel,并确保你没有丢弃文本字段中的原始值。在Luda的回答中实现的“取消”行为会用空字符串覆盖值,这可能不是你想要的。
- (void)viewDidLoad {
[super viewDidLoad];
float appWidth = CGRectGetWidth([UIScreen mainScreen].applicationFrame);
UIToolbar *accessoryView = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, appWidth, 0.1 * appWidth)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(selectDoneButton)];
accessoryView.items = @[space, done, space];
self.valueField.inputAccessoryView = accessoryView;
}
- (void)selectDoneButton {
[self.valueField resignFirstResponder];
}
有关构建附件视图的更多信息,请参阅Apple关于数据输入自定义视图的文档。你可能会想要参考UIToolbar和UIBarButtonItem的参考页面。
如果我们告诉视图控制器的视图结束编辑,我们也可以让“user touched somewhere else”解决方案更简单:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES]; //YES ignores any textfield refusal to resign
}
... 假设“触摸其他地方会取消键盘”是视图中任何其他可编辑字段所期望的行为。
一个更简单的解决方案
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[textViewInstance1 resignFirstResponder];
[textViewInstance2 resignFirstResponder];
[textField resignFirstResponder];
}
SWIFT 3.0不同的风格,使用了以前一些答案的部分内容。
func addToolbarToNumberPad()
{
let numberPadToolbar: UIToolbar = UIToolbar()
numberPadToolbar.isTranslucent = true
numberPadToolbar.items=[
UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelAction)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Custom", style: .done, target: self, action: #selector(self.customAction)),
UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.doneAction)),
]
numberPadToolbar.sizeToFit()
textField.inputAccessoryView = numberPadToolbar
}
func cancelAction()
{
textField.resignFirstResponder()
}
func customAction()
{
textField.resignFirstResponder()
}
func doneAction()
{
textField.resignFirstResponder()
}
override func viewDidLoad()
{
super.viewDidLoad()
self.addToolbarToNumberPad()
}
let pickerView = UIPickerView()
var yearpickerToolbar: UIToolbar?
func createPickerView() {
pickerView.delegate = self
textfield.inputView = pickerView }
func dismissPickerView() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
toolBar.isUserInteractionEnabled = true
textfield.inputAccessoryView = toolBar
textfield.delegate = self
yearpickerToolbar = UIToolbar()
yearpickerToolbar?.autoresizingMask = .flexibleHeight
//add buttons
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action:#selector(cancelBtnClicked(_:)))
cancelButton.tintColor = UIColor.blue
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .plain , target: self, action: #selector(self.doneBtnClicked(_ :) ))
doneButton.tintColor = UIColor.blue
doneButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
UIColor.black], for: .normal)
cancelButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
UIColor.black], for: .normal)
yearpickerToolbar?.items = [cancelButton, flexSpace, doneButton]
textfield.inputAccessoryView = yearpickerToolbar }
@objc func cancelBtnClicked(_ button: UIBarButtonItem?) {
self.view.endEditing(true)
}
@objc func doneBtnClicked(_ button: UIBarButtonItem?) {
self.view.endEditing(true)
}