问题
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?...
回答1:
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.
回答2:
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)
回答3:
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.
回答4:
I changed the tint color of all of my UITextViews and the cursor started showing on the next build.
回答5:
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];
}
回答6:
In my case, I called becomeFirstResponder in a viewDidLoad method. I moved the call into viewDidAppear and it worked for me.
回答7:
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.
回答8:
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
UIViewthat would represent the cursor in the correct position, but it does not appear in the UI. - right click the cursor's
UIViewand 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)
}
}
来源:https://stackoverflow.com/questions/20959331/cursor-not-showing-up-in-uitextview