【问题标题】:Cursor not appearing in UITextView iOS光标未出现在 UITextView iOS 中
【发布时间】:2017-10-15 04:12:10
【问题描述】:

我有一个带有几个 UITextField、一个 UILabel 和一个 UITextView 的可扩展 tableViewCell。当 tableView 展开标签消失并且 UITextFields 和 UITextView 出现。在两个 UITextFields 中都会出现光标,但在 UITextView 中没有光标出现。我已经尝试更改色调颜色并且没有任何东西,但是当我选择文本(确实出现)时,文本以色调颜色突出显示并且可见。我也试过改变框架,但没有奏效。此外,我现在将其设置为常规 UITextView,除了框架之外没有任何属性的更改。 textView 是使用 UITableViewCell 类以编程方式设置的,除了光标之外,一切似乎都正常工作。

这是我设置 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
}

这是类定义:

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
    }
}

有谁知道为什么光标没有出现?

提前感谢您的帮助。

以下是已选中光标的调试层次结构中的照片: Photo from the side

Photo from the front

光标前面的视图是placeholderField,并没有阻塞光标,因为光标在placeholderField下方时仍然没有出现。

【问题讨论】:

    标签: ios uitextview caret


    【解决方案1】:

    首先,我不知道你的代码是如何为你工作的,因为没有编译它我可以看到几个主要问题。

    • 您在哪里返回 Cell 中的 cellForRowAt indexPath
    • cell.teacherNameField 不在StudentTableViewCell

    坦率地说,按照您尝试编码的方式,这些事情并不难,只要保持简单并在编写代码之前思考(每一种可能的方法),那么只有您才能实现最佳实践。

    现在回到你的问题,我尝试了你的代码库,但事情正在重叠,你无法做到这一点,所以你是能解决它的最佳人选。

    使用Debug View Hierarchy

    然后您可以在屏幕上看到每个运行时视图层。

    【讨论】:

    • 我很抱歉我的代码。我评论了很多,看看这些属性是否正在改变,我不小心删除了一些本应包含在其中的行。我已经用我现在拥有的一切更新了我的代码。我还添加了 2 张照片,说明我在 Debug View Hierarchy 中实际看到的光标。我还打印了光标的描述,由于某种原因它的 alpha 为 0。有什么办法可以将 alpha 从 0 更改为 1 以使其可见。再次感谢您的帮助,我会在编写其他程序时牢记这些建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2014-04-14
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多