diff --git a/SwiftyCodeView.podspec b/SwiftyCodeView.podspec index 68e06d8..b57aa74 100644 --- a/SwiftyCodeView.podspec +++ b/SwiftyCodeView.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.swift_version = '5.0' s.name = 'SwiftyCodeView' - s.version = '0.3.4' + s.version = '0.3.5' s.summary = 'An UI Component for verification codes written in swift' s.description = <<-DESC diff --git a/SwiftyCodeView/Classes/SwiftyCodeView/SwiftyCodeView.swift b/SwiftyCodeView/Classes/SwiftyCodeView/SwiftyCodeView.swift index 03a1043..a0f0f0e 100644 --- a/SwiftyCodeView/Classes/SwiftyCodeView/SwiftyCodeView.swift +++ b/SwiftyCodeView/Classes/SwiftyCodeView/SwiftyCodeView.swift @@ -8,7 +8,7 @@ import UIKit @objc public protocol SwiftyCodeViewDelegate: class { - func codeView(sender: SwiftyCodeView, didFinishInput code: String) + func codeView(sender: SwiftyCodeView, didFinishInput code: String) -> Bool } @IBDesignable @@ -42,6 +42,9 @@ open class SwiftyCodeView: UIControl { let item = stackView.arrangedSubviews[i] as! SwiftyCodeItemView item.textField.text = i < array.count ? array[i] : "" } + if !stackView.arrangedSubviews.compactMap({$0 as? UITextField}).filter({$0.isFirstResponder}).isEmpty { + self.becomeFirstResponder() + } } } @@ -81,12 +84,14 @@ open class SwiftyCodeView: UIControl { .last as! SwiftyCodeItemView } + @discardableResult override open func becomeFirstResponder() -> Bool { let items = stackView.arrangedSubviews .map({$0 as! SwiftyCodeItemView}) return (items.filter({($0.textField.text ?? "").isEmpty}).first ?? items.last)!.becomeFirstResponder() } + @discardableResult override open func resignFirstResponder() -> Bool { stackView.arrangedSubviews.forEach({$0.resignFirstResponder()}) return true @@ -112,8 +117,9 @@ extension SwiftyCodeView: UITextFieldDelegate, SwiftyCodeTextFieldDelegate { item.textField.text = string sendActions(for: .valueChanged) if index == length - 1 { //is last textfield - delegate?.codeView(sender: self, didFinishInput: self.code) - textField.resignFirstResponder() + if (delegate?.codeView(sender: self, didFinishInput: self.code) ?? false) { + textField.resignFirstResponder() + } return false }