How to Add returnKey in NumberPad in Swift 3.0?


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 {
Step 6 : Adding button which title name is "Return"  Put code in ViewDidLoad. and make one method Done in which dismiss your keyboard code.
button.setTitle("Return", for: UIControlState())
button.setTitleColor(UIColor.black, for: UIControlState())
button.frame = CGRect(x: 0, y: 163, width: 106, height: 53)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: #selector(ViewController.Done(_:)), for: UIControlEvents.touchUpInside)
Step 7 : The delegate of textFieldDidBeginEditing adding notification to open KeyBoard code.
func textFieldDidBeginEditing(_ textField: UITextField) {
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
Step 8 : Code to open keyboard with animation.
func keyboardWillShow(_ note : Notification) -> Void{
    DispatchQueue.main.async { () -> Void in
        self.button.isHidden = false
        let keyBoardWindow = UIApplication.shared.windows.last
        self.button.frame = CGRect(x: 0, y: (keyBoardWindow?.frame.size.height)!-53, width: 106, height: 53)
        keyBoardWindow?.addSubview(self.button)
        keyBoardWindow?.bringSubview(toFront: self.button)
        UIView.animate(withDuration: (((note.userInfo! as NSDictionary).object(forKey: UIKeyboardAnimationCurveUserInfoKey) as AnyObject).doubleValue)!, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
            self.view.frame = self.view.frame.offsetBy(dx: 0, dy: 0)
            }, completion: { (complete) -> Void in
                print("Complete")
        })
    }
}
Step 9 : Keyboard with "Return" key.
Step 10 : Download Swift Demo : ReturnKey-SwiftDemo
Thanks
How to Add returnKey in NumberPad in Swift 3.0? How to Add returnKey in NumberPad in Swift 3.0? Reviewed by KIRIT MODI on 23:59:00 Rating: 5

4 comments:

  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)

    ReplyDelete
  2. Very Helpful tutorial. Thank You Kirit.

    ReplyDelete

Powered by Blogger.