在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
当前回答
我修改了Bryan的解决方案,使其更加健壮,这样它就可以很好地与可能出现在同一视图中的其他类型的键盘一起使用。描述如下:
在iOS numpad UIKeyboard上创建DONE按钮
我会试着在这里解释它,但大部分代码都是要查看的,不容易放在这里
其他回答
最简单的方法是:创建自定义透明按钮并将其放置在左下角,它将具有与UIKeyboardTypeNumberPad中的空白空间相同的CGSize。切换(显示/隐藏)这个按钮在textField成为firstresponder,按钮点击分别。
这是最新的代码。只包括 在你的viewController中导入"UIViewController+NumPadReturn.h"
这里是。h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIViewController (NumPadReturn)
@end
还有。m
#import "UIViewController+NumPadReturn.h"
@implementation UIViewController (NumPadReturn)
-(void) viewDidLoad{
// add observer for the respective notifications (depending on the os version)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)doneButton:(id)sender {
NSLog(@"doneButton");
[self.view endEditing:TRUE];
}
@end
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()
}
使用扩展的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()
}
Swift 2.2 /我用了Dx_的答案。然而,我希望所有的键盘都有这个功能。所以在我的基类中,我把代码:
func addDoneButtonForTextFields(views: [UIView]) {
for view in views {
if let textField = view as? UITextField {
let doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(dismissKeyboard))
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
textField.inputAccessoryView = doneToolbar
} else {
addDoneButtonForTextFields(view.subviews)
}
}
}
func dismissKeyboard() {
dismissKeyboardForTextFields(self.view.subviews)
}
func dismissKeyboardForTextFields(views: [UIView]) {
for view in views {
if let textField = view as? UITextField {
textField.resignFirstResponder()
} else {
dismissKeyboardForTextFields(view.subviews)
}
}
}
然后只需调用addDoneButtonForTextFields在self.view.subviews在viewDidLoad(或willDisplayCell如果使用表格视图)添加完成按钮到所有键盘。