0

I have a tableview cell with a textview inside it:

class DetailsCell: UITableViewCell, UITextViewDelegate {


    let detailsTextView: UITextView = {
        let tv = UITextView()
        tv.font = UIFont(name: "AvenirNext-Medium", size: 24)
        tv.isScrollEnabled = false
        tv.textColor = .white
        tv.backgroundColor = .clear
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()


    func textViewDidChange(_ textView: UITextView) {
        let addTodoViewController = AddTodoViewController()
        addTodoViewController.begindEndUpdate()
    }



    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: "DetailsCell")

        addSubview(detailsTextView)
        detailsTextView.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
        detailsTextView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        detailsTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
        detailsTextView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true


        backgroundColor = .black

        textLabel?.isHidden = true

        detailsTextView.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

When I load the app and the textview already has a text, assigned programmatically, the cell properly get's the right height and the textview is properly resized, but when I try to change the text of the textview, the cell and the textview don't resize. Can someone help me?

 override func viewWillAppear(_ animated: Bool) {
    toDoTextField.becomeFirstResponder()
    addTodoTableView.estimatedRowHeight = 60
    addTodoTableView.rowHeight = UITableViewAutomaticDimension
}

Textview with not initial text Image

Textview with initial text Image

1 Answers1

0

UITextView does not resize itself when its content changes. You have to write some code to resize your textView and then accordingly adjust the height of your cell. Probably this can help you resizing textview to its content

or you can use some third party library instead of default UITextView like this one GrowingTextView

Umair Aamir
  • 1,624
  • 16
  • 26