Reputation: 1791
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?...
Upvotes: 77
Views: 38266
Reputation: 11
I also faced same issue and also tried the given answers but was not able to see the cursor in textview. Because the something else was causing the issue. So some of you might have uncommon issue like me than this will be helpful to you.
I was having this method overrided which was causing the cursor visibility issue
override func caretRect(for position: UITextPosition) -> CGRect {
return .zero
}
So I just commented this lines and it was working fine for me
Upvotes: 0
Reputation: 61
a working crutch for me:
DispatchQueue.main.async {
self.textView.becomeFirstResponder()
self.textView.insertText(" ")
self.textView.deleteBackward()
}
Upvotes: 1
Reputation: 4762
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
}
Upvotes: 0
Reputation: 339
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:
UITextFieldView
UIView
that would represent the cursor in the correct position, but it does not appear in the UI.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)
}
}
Upvotes: 1
Reputation: 12919
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.
Upvotes: 0
Reputation: 2090
In my case, I called becomeFirstResponder
in a viewDidLoad
method. I moved the call into viewDidAppear
and it worked for me.
Upvotes: 1
Reputation: 4196
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];
}
Upvotes: 1
Reputation: 3074
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.
Upvotes: 4
Reputation: 73
I changed the tint color of all of my UITextViews and the cursor started showing on the next build.
Upvotes: 3
Reputation: 9337
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)
Upvotes: 9
Reputation: 20237
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.
Upvotes: 230