如何显示“完成”;iOS数字pad键盘上的按钮?

没有“完成”;按钮上的.numberPad键盘类型。当用户在文本字段中输入数字信息时,如何使数字pad消失?

我可以得到一个“完成”;按钮,但随后用户必须切换到数字键才能输入数字。有没有一种方法来显示“完成”;数字板上的按钮?

161468 次浏览

我所见过的技巧是创建一个与整个视图大小相同的自定义透明按钮,然后在其click方法中,让文本字段辞去first responder。因此,用户可以单击字段之外的任何地方来取消键盘。

UIKeyboardTypeNumberPad和缺少返回键中的解决方案工作得很好,但前提是屏幕上没有其他非数字填充文本字段。

我把这段代码变成一个UIViewController你可以简单地子类化它来让数字垫工作。您需要从上面的链接获取图标。

NumberPadViewController.h:

#import <UIKit/UIKit.h>


@interface NumberPadViewController : UIViewController {
UIImage *numberPadDoneImageNormal;
UIImage *numberPadDoneImageHighlighted;
UIButton *numberPadDoneButton;
}


@property (nonatomic, retain) UIImage *numberPadDoneImageNormal;
@property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted;
@property (nonatomic, retain) UIButton *numberPadDoneButton;


- (IBAction)numberPadDoneButton:(id)sender;


@end

和NumberPadViewController.m:

#import "NumberPadViewController.h"


@implementation NumberPadViewController


@synthesize numberPadDoneImageNormal;
@synthesize numberPadDoneImageHighlighted;
@synthesize numberPadDoneButton;


- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if ([super initWithNibName:nibName bundle:nibBundle] == nil)
return nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneUp3.png"];
self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneDown3.png"];
} else {
self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneUp.png"];
self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneDown.png"];
}
return self;
}


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];


// Add listener for keyboard display events
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];
}


// Add listener for all text fields starting to be edited
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidBeginEditing:)
name:UITextFieldTextDidBeginEditingNotification
object:nil];
}


- (void)viewWillDisappear:(BOOL)animated {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[super viewWillDisappear:animated];
}


- (UIView *)findFirstResponderUnder:(UIView *)root {
if (root.isFirstResponder)
return root;
for (UIView *subView in root.subviews) {
UIView *firstResponder = [self findFirstResponderUnder:subView];
if (firstResponder != nil)
return firstResponder;
}
return nil;
}


- (UITextField *)findFirstResponderTextField {
UIResponder *firstResponder = [self findFirstResponderUnder:[self.view window]];
if (![firstResponder isKindOfClass:[UITextField class]])
return nil;
return (UITextField *)firstResponder;
}


- (void)updateKeyboardButtonFor:(UITextField *)textField {


// Remove any previous button
[self.numberPadDoneButton removeFromSuperview];
self.numberPadDoneButton = nil;


// Does the text field use a number pad?
if (textField.keyboardType != UIKeyboardTypeNumberPad)
return;


// If there's no keyboard yet, don't do anything
if ([[[UIApplication sharedApplication] windows] count] < 2)
return;
UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];


// Create new custom button
self.numberPadDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.numberPadDoneButton.frame = CGRectMake(0, 163, 106, 53);
self.numberPadDoneButton.adjustsImageWhenHighlighted = FALSE;
[self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal];
[self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];


// Locate keyboard view and add button
NSString *keyboardPrefix = [[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 ? @"<UIPeripheralHost" : @"<UIKeyboard";
for (UIView *subView in keyboardWindow.subviews) {
if ([[subView description] hasPrefix:keyboardPrefix]) {
[subView addSubview:self.numberPadDoneButton];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];
break;
}
}
}


- (void)textFieldDidBeginEditing:(NSNotification *)note {
[self updateKeyboardButtonFor:[note object]];
}


- (void)keyboardWillShow:(NSNotification *)note {
[self updateKeyboardButtonFor:[self findFirstResponderTextField]];
}


- (void)keyboardDidShow:(NSNotification *)note {
[self updateKeyboardButtonFor:[self findFirstResponderTextField]];
}


- (IBAction)numberPadDoneButton:(id)sender {
UITextField *textField = [self findFirstResponderTextField];
[textField resignFirstResponder];
}


- (void)dealloc {
[numberPadDoneImageNormal release];
[numberPadDoneImageHighlighted release];
[numberPadDoneButton release];
[super dealloc];
}


@end

享受。

这是最新的代码。只包括 在你的viewController中导入"UIViewController+NumPadReturn.h"< / p >

这里是。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

我描述了iOS 4.2+ 在这里的一个解决方案,但键盘出现后,解散按钮会淡入。这并不可怕,但也不理想。

上面链接的问题中描述的解决方案包括一种更优雅的消除按钮的错觉,我将按钮淡出并垂直移动,以提供键盘和按钮一起消除的外观。

一个更简单的解决方案

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];


[textViewInstance1 resignFirstResponder];
[textViewInstance2 resignFirstResponder];
[textField resignFirstResponder];
}

简单的的方法是:

创建自定义透明按钮并将其放置在左下角,它将具有与UIKeyboardTypeNumberPad中的空白相同的CGSize。切换(显示/隐藏)此按钮在textField becomeFirstResponder上,在按钮上分别单击。

另一个解决方案。如果屏幕上有其他非数字填充文本字段,则完美。

inputAccessoryView

- (void)viewDidLoad
{
[super viewDidLoad];


UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}


-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}


-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}

这是我遇到的最简单的解决办法。我是从《iOS 5开发入门》这本书中学到的。

假设数字字段名为numberField

  1. ViewController中添加以下方法:

    -(IBAction)closeKeyboard:(id)sender;
    
  2. In ViewController.m, add the following code:

    -(IBAction)closeKeyboard:(id)sender
    {
    
    
    [numberField resignFirstResponder];
    
    
    }
    
  3. Go back to nib file.

  4. Open Utilities pan.
  5. Open the Identity inspector under Utilities pan.
  6. Click on the View (in nib file) once. Make sure you have not clicked on any of the items in the view. For the sake of clarification, you should see UIView under Class in Identity inspector.
  7. Change the class from UIView to UIControl.
  8. Open Connection Inspector.
  9. Click and drag Touch Down and drop the arrow on File Owner icon. (FYI... File Owner icon is displayed on the left of View and appears as a hollow cube with yellow frame.)
  10. Select the method: closeKeyboard.
  11. Run the program.

Now when you click anywhere on background of View, you should be able to dismiss the keyboard.

Hope this helps you solve your problem. :-)

我修改了Bryan的解决方案,使其更加健壮,这样它就可以很好地与可能出现在同一视图中的其他类型的键盘一起使用。描述如下:

在iOS numpad UIKeyboard上创建DONE按钮

我会试着在这里解释它,但大部分代码都是要查看的,不容易放在这里

如果你事先知道要输入的数字数量(例如一个4位数的PIN),你可以在按4次键后自动退出,就像我对这个类似问题的回答:

dismissing Number Pad

在这种情况下,不需要额外的done按钮。

以下是改编自Luda对斯威夫特的回答:

在你的UIViewController子类put的声明中

let numberToolbar: UIToolbar = UIToolbar()

在ViewDidLoad中放入:

    numberToolbar.barStyle = UIBarStyle.BlackTranslucent
numberToolbar.items=[
UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "hoopla"),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Apply", style: UIBarButtonItemStyle.Bordered, target: self, action: "boopla")
]


numberToolbar.sizeToFit()


textField.inputAccessoryView = numberToolbar //do it for every relevant textfield if there are more than one

并添加函数hoopla和hoopla(可以随意选择其他名称,只需相应地更改ViewDidLoad中的选择器名称

func boopla () {
textField.resignFirstResponder()
}


func hoopla () {
textField.text=""
textField.resignFirstResponder()
}

我们还可以让"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
}

... 假设“触摸其他地方会取消键盘”是视图中任何其他可编辑字段所期望的行为。

以下是对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文档数据输入的自定义视图。你可能还需要参考UIToolbarUIBarButtonItem的参考页面。

所有这些关于找到键盘视图并在第三行添加done按钮的实现(这就是为什么按钮。y = 163 b/c键盘的高度是216)是脆弱的,因为iOS不断改变视图层次结构。例如,上述代码都不适用于iOS9。

我认为更安全的方法是找到最顶层的视图,通过[[UIApplication sharedApplication] windows] lastObject],并在它的左下角添加按钮,doneButton.frame = CGRectMake(0, screen_height - 53,106,53);//人像模式

对于Swift 2.2,我使用这个

func addDoneButtonOnKeyboard() {
let doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))


let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: #selector(DetailViewController.finishDecimalKeypad))


var items: [UIBarButtonItem]? = [UIBarButtonItem]()
items?.append(flexSpace)
items?.append(done)


doneToolbar.items = items
doneToolbar.sizeToFit()
self.productPrice.inputAccessoryView=doneToolbar
}


func finishDecimalKeypad() {
self.productPrice?.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如果使用表格视图)添加完成按钮到所有键盘。

如果你有多个数字字段,我建议子类化UITextField来创建一个NumericTextField,它总是显示一个带有done按钮的数字键盘。然后,简单地将你的数值字段与接口构建器中的这个类关联起来,你就不需要在任何视图控制器中添加任何额外的代码。下面是我在Xcode 8.0中使用的Swift 3.0类。

class NumericTextField: UITextField {
let numericKbdToolbar = UIToolbar()


// MARK: Initilization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}


override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}


// Sets up the input accessory view with a Done button that closes the keyboard
func initialize()
{
self.keyboardType = UIKeyboardType.numberPad


numericKbdToolbar.barStyle = UIBarStyle.default
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let callback = #selector(NumericTextField.finishedEditing)
let donebutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: callback)
numericKbdToolbar.setItems([space, donebutton], animated: false)
numericKbdToolbar.sizeToFit()
self.inputAccessoryView = numericKbdToolbar
}


// MARK: On Finished Editing Function
func finishedEditing()
{
self.resignFirstResponder()
}
}

斯威夫特4.2

class NumericTextField: UITextField {
let numericKbdToolbar = UIToolbar()


// MARK: Initilization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}


override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}


// Sets up the input accessory view with a Done button that closes the keyboard
func initialize()
{
self.keyboardType = UIKeyboardType.numberPad


numericKbdToolbar.barStyle = UIBarStyle.default
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let callback = #selector(NumericTextField.finishedEditing)
let donebutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: callback)
numericKbdToolbar.setItems([space, donebutton], animated: false)
numericKbdToolbar.sizeToFit()
self.inputAccessoryView = numericKbdToolbar
}


// MARK: On Finished Editing Function
@objc func finishedEditing()
{
self.resignFirstResponder()
}
}

我发现@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)
}

使用扩展的斯威夫特3解决方案。如果你的应用程序中有几个数值UITextField对象是理想的,因为它可以灵活地决定,对于每个UITextField,当完成取消被点击时是否执行自定义操作。

enter image description here

//
//  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()
}

斯威夫特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)
}