在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
当前回答
这是最新的代码。只包括 在你的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
其他回答
我所见过的技巧是创建一个与整个视图大小相同的自定义透明按钮,然后在其click方法中,让文本字段辞去first responder。因此,用户可以单击字段之外的任何地方来取消键盘。
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如果使用表格视图)添加完成按钮到所有键盘。
最简单的方法是:创建自定义透明按钮并将其放置在左下角,它将具有与UIKeyboardTypeNumberPad中的空白空间相同的CGSize。切换(显示/隐藏)这个按钮在textField成为firstresponder,按钮点击分别。
如果你事先知道要输入的数字数量(例如一个4位数的PIN),你可以在按4次键后自动退出,就像我对这个类似问题的回答:
取消数字垫
在这种情况下,不需要额外的done按钮。
我发现@user1258240的答案是非常简洁的,因为这并不像设置一个returnKeyType属性那么简单。
只是想贡献我自己的“可重用”方法:
func SetDoneToolbar(field:UITextField) {
let doneToolbar:UIToolbar = UIToolbar()
doneToolbar.items=[
UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(ViewController.dismissKeyboard))
]
doneToolbar.sizeToFit()
field.inputAccessoryView = doneToolbar
}
override func viewDidLoad() {
super.viewDidLoad()
SetDoneToolbar(field: UITextField_1)
SetDoneToolbar(field: UITextField_2)
SetDoneToolbar(field: UITextField_3)
SetDoneToolbar(field: UITextField_N)
}