【问题标题】:How to add Done button on numberpad keyboard for VPMOTPView in swift如何在 Swift 中为 MAPView 的数字键盘键盘上添加完成按钮
【发布时间】:2023-03-15 03:05:01
【问题描述】:

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

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

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

【问题讨论】:

  • 添加第三方库 pod 'IQKeyboardManagerSwift',完成按钮就会出现。提前致谢。
  • 乐于助人为你加油

标签: ios swift view keyboard


【解决方案1】:

首先在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()
    }
}

现在,在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()
}

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 2012-04-22
      • 2016-03-19
      • 2013-12-10
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多