SeanT
SeanT

Reputation: 1791

Cursor not showing up in UITextView

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

Answers (11)

Sanket Parmar
Sanket Parmar

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

Dima
Dima

Reputation: 61

a working crutch for me:

   DispatchQueue.main.async {
           
      self.textView.becomeFirstResponder()
      self.textView.insertText(" ")
      self.textView.deleteBackward()
 }

Upvotes: 1

bahadir arslan
bahadir arslan

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

TolkienWASP
TolkienWASP

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:

  1. select your UITextFieldView
  2. 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.
  3. 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)
    }
}

Upvotes: 1

FD_
FD_

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

Vladimir Vlasov
Vladimir Vlasov

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

Johnny Rockex
Johnny Rockex

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

Kunal Gupta
Kunal Gupta

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.

enter image description here

Upvotes: 4

Andy Bernard
Andy Bernard

Reputation: 73

I changed the tint color of all of my UITextViews and the cursor started showing on the next build.

Upvotes: 3

Julian
Julian

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

lehn0058
lehn0058

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

Related Questions