fixed bug in keyboard

updated version

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-07-21 16:54:47 -05:00
parent d4b1de0f38
commit 7c2836be6d
3 changed files with 36 additions and 23 deletions

View File

@ -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;

View File

@ -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
}
}

View File

@ -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
}