Cursor not showing up in UITextView
IosIphoneIpadUitextfieldUitextviewIos Problem Overview
Can anyone think of a reason that the blinking cursor would not show up in a UITextView
? I have a custom control that is just a subclass of UIView
and it has a UITextView
in it, but the cursor doesn't appear when it gets focus. The keyboard appears, and the text appears as you type, but there is no cursor, and I can't figure out why.
Any thoughts?...
Ios Solutions
Solution 1 - Ios
You may have changed the tint color in your custom UITextView. If the tint color is the same as the background color (normally white), then it will appear invisible.
Solution 2 - Ios
You might be setting improperly a contentSize
and/or frame for the component so it is too small to be visible or the control is out of the screen. Please go in Simulator to Debug->Color Blended Layers
to see whether those values are set correctly.
EDIT:
With new Xcodes (probably introduced in Xcode 6) you can debug this kind of issues by clicking "Debug View Hierarchy" (it is one of the icons on the bottom bar)
Solution 3 - Ios
The textfield is showing the cursor but you are not able to see the color, just because the tint color of your text field is set to default most probably as in my case it was. Just select you textField in storyboard and Select the link color of your wish. Refer to the image attached.
Solution 4 - Ios
I changed the tint color of all of my UITextViews and the cursor started showing on the next build.
Solution 5 - Ios
Simple workaround seems to work by introducing a delay and then calling firstResponder, like so:
-(void)begin{
[self performSelector:@selector(first) withObject:nil afterDelay:0.01f];
}
-(void)first{
[tf becomeFirstResponder];
}
Solution 6 - Ios
In my case, I called becomeFirstResponder
in a viewDidLoad
method. I moved the call into viewDidAppear
and it worked for me.
Solution 7 - Ios
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 indicatesOPAQUE=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)
}
}
Solution 8 - Ios
For me, the problem was caused by embedding a UITextField
in a UIToolbar
. Once I had replaced the container for a simple UIView
, the cursor worked again.
Solution 9 - Ios
For me it wasn't the tintColor, it was caused by overriding caretRect method. Removing this method solved my case
open override func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}
Solution 10 - Ios
a working crutch for me:
DispatchQueue.main.async {
self.textView.becomeFirstResponder()
self.textView.insertText(" ")
self.textView.deleteBackward()
}