ios - 如何在数字键盘上快速为 VPMOTPView 添加完成按钮
问题描述
我将 VPMOTPView 用于 OTP 字段,当我点击此处的 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()
}
}
请帮助我在键盘上添加完成的代码。
解决方案
首先介绍一下这个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()
}
推荐阅读
- java - Web3j 安装后运行异常
- java - 删除单对多关系jpa中的实体
- elixir - 保护伞下的应用程序之间的 Phoenix PubSub
- android - 如何从 MutableLiveData 正确绑定到 EditText
- python - 尝试在 python 中构建二叉搜索树时输入错误
- python - text io wrapper error 事情我很困惑
- node.js - 部署到 HEROKU 时,路由到新页面时仅从后端获取数据
- c# - 如何使用异步 GRPC 调用有效地管理内存
- java - 使用 slick util 将文本绘制到屏幕上
- excel - 使用匹配的数字前缀添加新工作表