Alexander Arovas
Alexander Arovas

Reputation: 1

Cursor not appearing in UITextView iOS

I have an expandable tableViewCell with a couple UITextFields, a UILabel and a UITextView. When the tableView expands the label disappears and the UITextFields and UITextView appear. In both UITextFields the cursor appears but in the UITextView no cursor appears. I have tried changing the tint color and nothing shoed up, however when I select text (which does appear) the text is highlighted in the tint color and is visible. I have also tried changing the frame but that didn't work. Also I have it setup now as a regular UITextView with no changes to any properties other than the frame. The textView is setup programmatically using a UITableViewCell class and everything seems to work but the cursor.

Here's how I set up the UITextView:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StudentTableViewCell

    cell.infoView.frame = CGRect(x: 23, y: 207, width: cell.frame.width - 36, height: cell.frame.height - 155)
    cell.infoView.delegate = self
    cell.infoView.text = classes[indexPath.row].info
    cell.infoView.layer.cornerRadius = 5
    cell.infoView.layer.masksToBounds = true

    cell.placeholderField.frame = CGRect(x: 32, y: 92, width: self.view.frame.width - 38, height: 45)
    if cell.infoView.text!.replacingOccurrences(of: " ", with: "") == "" {
        cell.placeholderField.placeholder = "Description"
    } else {
        cell.placeholderField.placeholder = ""
    }
    cell.placeholderField.backgroundColor = UIColor.clear

    cell.nameField.frame = CGRect(x: 23, y: 3, width: cell.frame.width - 70, height: 44)
    cell.nameField.text = classes[indexPath.row].name
    cell.nameField.delegate = self
    cell.nameField.layer.cornerRadius = 5
    cell.nameField.layer.masksToBounds = true
    cell.nameField.borderStyle = .roundedRect

    cell.teacherField.frame = CGRect(x: 23, y: 50, width: cell.frame.width - 36, height: 44)
    cell.teacherField.text = classes[indexPath.row].teacher.name
    cell.teacherField.delegate = self
    cell.teacherField.layer.cornerRadius = 5
    cell.teacherField.layer.masksToBounds = true
    cell.teacherField.borderStyle = .roundedRect

    cell.editButton.frame = CGRect(x: self.view.frame.width - 60, y: 15, width: 70, height: 20)
    cell.editButton.addTarget(self, action: #selector(self.editInfoView), for: .touchUpInside)
    cell.editButton.setTitle(cell.editButton.title(for: .normal) ?? "Edit", for: .normal)

    cell.contentView.addSubview(cell.nameField)
    cell.contentView.addSubview(cell.teacherField)
    cell.contentView.addSubview(cell.infoView)
    cell.contentView.addSubview(cell.editButton)
    cell.contentView.addSubview(cell.placeholderField)

    cell.textLabel?.text = classes[indexPath.row].name
    cell.detailTextLabel?.text = classes[indexPath.row].teacher.name

    return cell
}

Here is the Class definition:

class StudentTableViewCell: UITableViewCell {
    var infoView = UITextView()
    var placeholderField = UITextField()
    var nameField = UITextField()
    var teacherField = UITextField()
    var editButton = UIButton()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

Does anyone know why the cursor isn't appearing?

Thanks in advance for the help.

Here are photos from the debug hierarchy with the cursor selected: Photo from the side

Photo from the front

The view in front of the cursor is the placeholderField and is not blocking the cursor because the cursor still doesn't appear when it is below the placeholderField.

Upvotes: 0

Views: 621

Answers (1)

Buntylm
Buntylm

Reputation: 7373

First of all, I don't know how's your code is working for you as without compiling it I can see few major issues like.

  • Where you are returning the Cell in cellForRowAt indexPath?
  • cell.teacherNameField is not there in StudentTableViewCell

And frankly speaking, these things are not difficult they way you are trying to code, Just keep it simple and think before writing code (each and every possible approach) then only you will achieve the best practice.

Now coming back to your question, I tried with your codebase and thing are getting overlap that's you are not able to do this, so you are the best person who can solve it.

Use Debug View Hierarchy

enter image description here

And then you can see each and every runtime view layer on your screen.

enter image description here

Upvotes: 1

Related Questions