如何快速在数字键盘上为VPMOTPView添加完成按钮

问题描述 投票:1回答:2

我正在对OTP字段使用VPMOTPView,当我在OTP视图上点击时,我正在使用带数字键盘的键盘,但是在这里如何将完成按钮添加到键盘。

enter image description here

我能够在文本字段的键盘上添加完成按钮,但是如何为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()
}
}

[请帮助我提供在键盘上添加完成的代码。

ios swift view keyboard
2个回答
1
投票

首先在extension上引入此UITextField以添加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()
    }
}

现在,如下对addDoneButtonOnKeyboard中使用的所有UITextField实例调用VPMOTPView方法,

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

0
投票

您可以在工具栏的顶部添加带有完成按钮的工具栏。

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
}
}

导入的部分是UITextField上的inputAccessoryView

© www.soinside.com 2019 - 2024. All rights reserved.