Сначала введите 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()
}