简体   繁体   English

如何在 swift 中为 VPMOTPView 的数字键盘上添加完成按钮

[英]How to add Done button on numberpad keyboard for VPMOTPView in swift

I am using VPMOTPView for OTP fields, when i tap on the OTP view here i am getting keyboard with numberpad but here how to add Done Button to keyboard.我将 VPMOTPView 用于 OTP 字段,当我点击此处的 OTP 视图时,我正在获取带数字键盘的键盘,但此处如何将完成按钮添加到键盘。

在此处输入图像描述

I am able to add Done button on keyboard for textfield, but how to add for VPMOTPView.我可以在键盘上为文本字段添加完成按钮,但如何为 VPMOTPView 添加。

class OTPViewController: UIViewController {

@IBOutlet weak var otpView: VPMOTPView!
var phone : String?
var otp   : String?

override func viewDidLoad() {
    super.viewDidLoad()
    //self.navigationBarButton()
    otpView.otpFieldsCount = 6
    otpView.otpFieldDefaultBorderColor = UIColor.lightGray
    otpView.otpFieldEnteredBorderColor = UIColor(named: "LightPrimaryColor") ?? UIColor.blue
    otpView.otpFieldErrorBorderColor = UIColor.red
    otpView.otpFieldBorderWidth = 1
    otpView.delegate = self
    otpView.shouldAllowIntermediateEditing = false
    otpView.otpFieldSize = 25
    otpView.otpFieldDisplayType = .underlinedBottom
    otpView.otpFieldEntrySecureType=false
    otpView.initializeUI()
    emailIconLabel.text = "We have sent an sms with OTP \nto \(phone!)"
    self.getOTPService()
}
}

Please help me with the code for adding Done on keyboard.请帮助我在键盘上添加完成的代码。

First introduce this extension on UITextField to add Done button.首先在UITextField上引入这个extension来添加Done按钮。

extension UITextField {

    /// Adding a done button on the keyboard
    func addDoneButtonOnKeyboard() {
        let doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        doneToolbar.barStyle = .default

        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))

        let items = [flexSpace, done]
        doneToolbar.items = items
        doneToolbar.sizeToFit()

        self.inputAccessoryView = doneToolbar
    }

    /// Done button callback
    @objc func doneButtonAction() {
        self.resignFirstResponder()
    }
}

Now, call the addDoneButtonOnKeyboard method on all the UITextField instances used in the VPMOTPView as below,现在,在VPMOTPView中使用的所有UITextField实例上调用addDoneButtonOnKeyboard方法,如下所示,

override func viewDidLoad() {
    super.viewDidLoad()
    //self.navigationBarButton()
    otpView.otpFieldsCount = 6
    otpView.otpFieldDefaultBorderColor = UIColor.lightGray
    otpView.otpFieldEnteredBorderColor = UIColor(named: "LightPrimaryColor") ?? UIColor.blue
    otpView.otpFieldErrorBorderColor = UIColor.red
    otpView.otpFieldBorderWidth = 1
    otpView.delegate = self
    otpView.shouldAllowIntermediateEditing = false
    otpView.otpFieldSize = 25
    otpView.otpFieldDisplayType = .underlinedBottom
    otpView.otpFieldEntrySecureType=false
    otpView.initializeUI()
    emailIconLabel.text = "We have sent an sms with OTP \nto \(phone!)"

    otpView.subviews.compactMap({ $0 as? VPMOTPTextField}).forEach { tv in
        tv.addDoneButtonOnKeyboard()
    }
    self.getOTPService()
}

You can append a toolbar at top of the keyboard with a done button.您可以在键盘顶部的工具栏上使用完成按钮。

extension UITextField {

func addDoneButtonOnKeyboard() {
    let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
    doneToolbar.barStyle = .default
    let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(someMethod))
    let items = [flexSpace, done]
    doneToolbar.items = items
    doneToolbar.sizeToFit()
    inputAccessoryView = doneToolbar
}
}

The imported part is inputAccessoryView on your UITextField.导入的部分是inputAccessoryView上的 inputAccessoryView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM