How to Add returnKey in NumberPad in Swift?

In Keyboard, There are return key to dismiss the window of keyboard, But when you selected Keyboard type is Number Pad then there isn't return key, So how can dismiss without return key ?

Here, We are putting Return key on Number Pad using programmatically to dismiss keyboard window. Show the steps and at last demo of its.

Step 1 : Create new project Xcode > File > New > Project > ReturnKeyDemo
Step 2 : Adding one textField named as txtReturn and also give its delegate.
Step 3 : Go to text field  property and selected keyBoard type is : Number Pad.
Step 4 : Now Build and Run project. KeyBoard haven't return key.
Step 5 : Now adding textField delegate at ViewController class.
class ViewController: UIViewController , UITextFieldDelegate {
Setp 6 : Adding button which title name is "Return"  Put code in ViewDidLoad. and meke one method Done in which dismiss your keyboard code.

let button = UIButton(type: UIButtonType.Custom)
button.setTitle("Return", forState: UIControlState.Normal)
button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
button.frame = CGRectMake(0, 163, 106, 53)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: "Done:", forControlEvents: UIControlEvents.TouchUpInside)
Step 7 : The delegate of textFieldDidBeginEditing adding notification to open KeyBoard code.
func textFieldDidBeginEditing(textField: UITextField) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
Step 8 : Code to open keyboard with aniamtion.

func keyboardWillShow(note : NSNotification) -> Void{
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.button.hidden = false
            let keyBoardWindow = UIApplication.sharedApplication().windows.last
            self.button.frame = CGRectMake(0, (keyBoardWindow?.frame.size.height)!-53, 106, 53)
            UIView.animateWithDuration(((note.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationCurveUserInfoKey)?.doubleValue)!, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                self.view.frame = CGRectOffset(self.view.frame, 0, 0)
                }, completion: { (complete) -> Void in
Step 9 : Keyboard with "Return" key.
Step 10 : Download Swift Demo : ReturnKey-SwiftDemo

How to Add returnKey in NumberPad in Swift? How to Add returnKey in NumberPad in Swift? Reviewed by Kirit Modi on 23:59:00 Rating: 5


  1. Change this to adjust the x coordinates of the return button in all devices :
    let doneButtonXCoordinates = (keyBoardWindow?.frame.size.width)! * CGFloat(0.33125)
    self.button.frame = CGRectMake(0, (keyBoardWindow?.frame.size.height)!-53, doneButtonXCoordinates, 53)

  2. Very Helpful tutorial. Thank You Kirit.


Powered by Blogger.