I have such view hierarchy:
View Controller
UITableView
UITableViewCell
UITextField
I have in my custom UITableViewCell
a textField
and my plan is to make this textField
becomeFirstResponder as soon as user tap on cell. The first problem is that textField
is getting touches events and cell is not selected because of that so when user tap right onto the textField
then cell is not selected and I'm not able to update selected cell UI. I though that I can make isUserInteractionEnabled=false
on textField
so after user taps on textField
I will have proper selection behavior in my cells. So far so good. Now I want to handle this selection in setSelected
method in my custom cell. The problem is that if keyboard is already on screen and I execute becomeFirstResponder()
on textField
then the keyboard is hiding and immediately showing again. Normally I expect that this keyboard will stay on its place without trying to hide and show again. Here is the code:
class StringFieldTableViewCell: UITableViewCell {
@IBOutlet weak var pinLine: UIView!
@IBOutlet weak var pinLineFocused: UIView!
@IBOutlet weak var textField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .none
self.textField.isUserInteractionEnabled = false
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.textField.isUserInteractionEnabled = selected
if selected {
self.textField.becomeFirstResponder()
}
self.pinLine.isHidden = selected
self.pinLineFocused.isHidden = !selected
}
}
Does anybody knows how to fix this keyboard showing/hiding problem and why keyboard tries first to hide itself and then show in very short time?