From 7c2836be6dc5e84a11524eeb06c931294e19ed12 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 21 Jul 2023 16:54:47 -0500 Subject: [PATCH] fixed bug in keyboard updated version Signed-off-by: Matt Bruce --- VDSSample.xcodeproj/project.pbxproj | 4 ++-- VDSSample/Classes/Slider.swift | 18 +++++++------- VDSSample/Classes/TextField.swift | 37 ++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/VDSSample.xcodeproj/project.pbxproj b/VDSSample.xcodeproj/project.pbxproj index 345607c..d925ed0 100644 --- a/VDSSample.xcodeproj/project.pbxproj +++ b/VDSSample.xcodeproj/project.pbxproj @@ -675,7 +675,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 30; + CURRENT_PROJECT_VERSION = 31; DEVELOPMENT_TEAM = FCMA4QKS77; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = VDSSample/Info.plist; @@ -707,7 +707,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 30; + CURRENT_PROJECT_VERSION = 31; DEVELOPMENT_TEAM = FCMA4QKS77; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = VDSSample/Info.plist; diff --git a/VDSSample/Classes/Slider.swift b/VDSSample/Classes/Slider.swift index d702c48..fd64d25 100644 --- a/VDSSample/Classes/Slider.swift +++ b/VDSSample/Classes/Slider.swift @@ -9,7 +9,7 @@ import Foundation import UIKit import VDS -class Slider: Control, UITextFieldDelegate { +class Slider: Control { var textField = NumericField().with { $0.translatesAutoresizingMaskIntoConstraints = false } var range = UISlider().with { $0.translatesAutoresizingMaskIntoConstraints = false } var maximumValue: Float = 0.0 { didSet { range.maximumValue = maximumValue }} @@ -19,7 +19,6 @@ class Slider: Control, UITextFieldDelegate { addSubview(textField) addSubview(range) textField.pinTop() - textField.delegate = self textField.pinBottom() textField.pinLeading() textField.heightAnchor.constraint(equalToConstant: 44).isActive = true @@ -33,6 +32,13 @@ class Slider: Control, UITextFieldDelegate { range.publisher(for: .valueChanged).sink { [weak self] slider in self?.valueChanged(newValue: slider.value) }.store(in: &subscribers) + + textField.resignAction = { [weak self] textField in + guard let self else { return } + if let text = textField.text, let n = NumberFormatter().number(from: text) { + valueChanged(newValue: n.floatValue) + } + } } override func updateView() { @@ -43,14 +49,6 @@ class Slider: Control, UITextFieldDelegate { value = newValue sendActions(for: .valueChanged) } - - func textFieldShouldReturn(_ textField: UITextField) -> Bool { - textField.resignFirstResponder() - if let text = textField.text, let n = NumberFormatter().number(from: text) { - valueChanged(newValue: n.floatValue) - } - return true - } } diff --git a/VDSSample/Classes/TextField.swift b/VDSSample/Classes/TextField.swift index 11f4713..ad68936 100644 --- a/VDSSample/Classes/TextField.swift +++ b/VDSSample/Classes/TextField.swift @@ -12,6 +12,9 @@ import VDSFormControlsTokens import Combine public class TextField: UITextField { + public var resigner: AnyCancellable? + + public var resignAction: ((TextField) -> Void)? public var isNumeric: Bool = false @@ -33,19 +36,13 @@ public class TextField: UITextField { } public func setup() { - let keypadToolbar: UIToolbar = UIToolbar() - - // add a done button to the numberpad - keypadToolbar.items=[ - UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil), - UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(UITextField.resignFirstResponder)) - ] - keypadToolbar.sizeToFit() - - // add a toolbar with a done button above the number pad - inputAccessoryView = keypadToolbar keyboardType = .alphabet returnKeyType = .done + + resigner = publisher(for: .editingDidEndOnExit) + .sink { [weak self] _ in + self?.shouldResign() + } } public override func textRect(forBounds bounds: CGRect) -> CGRect { @@ -61,6 +58,13 @@ public class TextField: UITextField { let rect = super.editingRect(forBounds: bounds) return rect.inset(by: textPadding) } + + @objc public func shouldResign() { + if let resignAction { + resignAction(self) + } + resignFirstResponder() + } } public class NumericField: TextField { @@ -75,6 +79,17 @@ public class NumericField: TextField { public override func setup() { super.setup() + let keypadToolbar: UIToolbar = UIToolbar() + + // add a done button to the numberpad + keypadToolbar.items=[ + UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil), + UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(shouldResign)) + ] + keypadToolbar.sizeToFit() + + // add a toolbar with a done button above the number pad + inputAccessoryView = keypadToolbar keyboardType = .numberPad }