The solution above did not work in my case. The issue in my case is that the UIView
that acts as the cursor has isOpaque
set to false
when the UITableViewCell
's selectionStyle
is anything but none
. This seems to only occur under a race condition I haven't gotten to the bottom of yet.
FWIW here's how I debugged this:
- select your
UITextFieldView
- open the Debug View Hierarchy. You should see the
UIView
that would represent the cursor in the correct position, but it does not appear in the UI.
- right click the cursor's
UIView
and select "Print Description...". The description indicates OPAQUE=NO
Unfortunately, the UITextSelectionView
class that owns this UIView
is private and therefore there is no non-hacky way to programmatically update isOpaque
.
This doesn't seem to be an issue in every implementation, however, here's the relationship between my views.
My text view delegate
extension FooViewController: UITextViewDelegate {
private func updateSelection(selectedIndex indexPath: IndexPath) {
if let oldSelected = self.table.indexPathForSelectedRow {
tableDelegate.tableView(table, didDeselectRowAt: oldSelected)
}
tableDelegate.tableView(table, didSelectRowAt: indexPath)
}
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
guard let rowCell = getParentCell(forTextView: textView), // loops through table row cells
!rowCell.isSelected,
let indexPath = self.table.indexPath(for: rowCell)
else { return true }
updateSelection(selectedIndex: indexPath)
return false
}
and my table delegate:
class TableViewDelegate: NSObject, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
}
}