How to do number keypad with done button in Swift 2.0?

时光毁灭记忆、已成空白 提交于 2019-12-23 02:59:19

问题


I've tried lots of numeric keyboard with done button, it's not found a correct answer in SWIFT 2.0.


回答1:


You can add done button above keyboard using toolbar

override func viewDidLoad() {
    super.viewDidLoad()
    let tooBar: UIToolbar = UIToolbar()
    tooBar.barStyle = UIBarStyle.BlackTranslucent
    tooBar.items=[
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil),
        UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed")]
    tooBar.sizeToFit()
    yourTextFeild.inputAccessoryView = tooBar
}

func donePressed () {
    yourTextFeild.resignFirstResponder()
}

It will look like this




回答2:


Without toolbar, I'd added the done button in the Return key place itself by the following steps,

Create a custom Return button as below

let mobilePadNextButton = UIButton(type: UIButtonType.custom)

In viewdidload, design it. Here I'm designing Next button as I need Next instead of Done.

mobilePadNextButton.setTitle("Next", for: UIControlState())//Set Done here
mobilePadNextButton.setTitleColor(UIColor.black, for: UIControlState())
mobilePadNextButton.frame = CGRect(x: 0, y: 163, width: 106, height: 53)
mobilePadNextButton.adjustsImageWhenHighlighted = false
mobilePadNextButton.addTarget(self, action: #selector(self.mobilePadNextAction(_:)), for: UIControlEvents.touchUpInside)

Add Keyboad notification

func textFieldDidBeginEditing(_ textField: UITextField) {
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}


func keyboardWillShow(notification:NSNotification){

    if mobileNoTxtFld.isFirstResponder
    {
        DispatchQueue.main.async { () -> Void in
            self.mobilePadNextButton.isHidden = false
            let keyBoardWindow = UIApplication.shared.windows.last
            self.mobilePadNextButton.frame = CGRect(x: 0, y: (keyBoardWindow?.frame.size.height)!-53, width: 106, height: 53)
            keyBoardWindow?.addSubview(self.mobilePadNextButton)
            keyBoardWindow?.bringSubview(toFront: self.mobilePadNextButton)

            UIView.animate(withDuration: (((notification.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")
            })
        }
    }
    else
    {
        self.mobilePadNextButton.isHidden = true
    }

}

Here, you can do the done actions

func mobilePadNextAction(_ sender : UIButton){

    //Click action

}

The UI will looks like below,

I referred this Git Code



来源:https://stackoverflow.com/questions/41561929/how-to-do-number-keypad-with-done-button-in-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!