ios - 如何快速在 MAPView 的数字键盘上添加完成按钮

标签 ios swift view keyboard

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

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

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

最佳答案

先介绍一下这个extensionUITextField添加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()
}

关于ios - 如何快速在 MAPView 的数字键盘上添加完成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245897/

相关文章:

swift - 在 Xcode 模拟器 [swift 3.0] 上启动时屏幕为空白

swift - 从字符串转换为二进制,然后转换为 base64 格式

objective-c - UIPopover 不显示内部 View

ios - 如何在 React Native 应用程序中使用 GameCenter

iOS、SpriteKit 将 SKTextureAtlas/SKTexture 转换为 UIImage

ios - 如何获得滚动移动的长度

sorting - 订购 Backbone View 和集合

java - 在 Activity 类中使用哪个上下文?

android - 在 LinearLayout 中设置 EditText 宽度取决于 TextView 宽度

ios - 将带通滤波器快速应用于 float 数组