I currently have a UIView with a TableView and a TextField. In my table view I create EndTimeCell (below), which have a selector set to trigger on the UISwitch value change. However, if the keyboard is visible, when I tap on the UISwitch, it changes, but the selector does not fire. Does anyone have any idea why?
I've attempted to modify the UIGestureRecognizerDelegate to allow the touch to pass through but since I create the cells in the table view delegate, I have no way of checking if the tap is on the view element.
I've also tried resigning the keyboard, but it does nothing.
To be clear, the selector triggers fine if the keyboard is not showing, but does not fire if it is.
Thanks!
class EndTimeCell: UITableViewCell {
var label: UILabel = {
let lbl = UILabel()
lbl.text = "End"
lbl.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
lbl.textColor = UIColor.gray
return lbl
}()
var enable: UISwitch = {
let en = UISwitch()
en.addTarget(self, action: #selector(onSwitchValueChanged(_:)), for: .valueChanged)
return en
}()
var callback: (_ state: Bool) -> Void = {state in}
@objc func onSwitchValueChanged(_ toggle: UISwitch) {
print("CHANGED CHANGED CHANGED")
if toggle.isOn {
label.textColor = UIColor(named: "Secondary")
} else {
label.textColor = UIColor.gray
}
callback(toggle.isOn)
}
func layoutView() {
label.translatesAutoresizingMaskIntoConstraints = false
enable.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.centerYAnchor),
label.leftAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.leftAnchor, constant: Constants.padding + Constants.textFieldCornerRadius),
enable.centerYAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.centerYAnchor),
enable.rightAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.rightAnchor, constant: -Constants.padding),
])
}
func setCallback(callback: @escaping (_ state: Bool) -> Void) {
self.callback = callback
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(label)
contentView.addSubview(enable)
layoutView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
contentView.addSubview(label)
contentView.addSubview(enable)
layoutView()
}
override func layoutSubviews() {
super.layoutSubviews()
layoutView()
}
}