0

I have (3) UITextViews, (1) UITextField and (1) UILabel within a single UICollectionViewCell. I have all the textViews changing in height when the text wraps onto a second line with a textViewDidChange function in an extension that conforms to the UITextViewDelegate protocol:

    func textViewDidChange(_ textView: UITextView) {
        let size = CGSize(width: frame.width, height: .infinity)
        let estimatedSize = textView.sizeThatFits(size)
        textView.constraints.forEach { (constraint) in
            if constraint.firstAttribute == .height {
                constraint.constant = estimatedSize.height
            }
        }
    }

Ideally, whenever any of the textViews in my cell increase in height, I'd like that to increase the height of the cell.

I tried putting:

self.frame.size.height += estimatedSize.height

in the if constraint.firstAttribute == .height section, but that only increased the height of the cell until it reaches the bottom of the screen. And I'd like the user to be able to enter as much text as they'd like.

Is there a loop I should be using to edit the height of the cell whenever this textViewDidChange changes the height of a textView?

I'm super stumped, so any help would be awesome! Thanks in advance!

CAVEAT:

  • After alot of searching, I found a bunch of answers that show how to dynamically increase the height of a cell. However, I don't know how to make unique cells within a UICollectionView. If the most elegant solution here is to put all the textViews, the textField, and the label into unique cells, please show me how to do that!
pozzy
  • 167
  • 1
  • 1
  • 10
  • There are tons of tutorials and SO posts which show you how to do that. Check this - https://stackoverflow.com/questions/25895311/uicollectionview-self-sizing-cells-with-auto-layout – Rakesha Shastri Feb 05 '19 at 05:00
  • @RakeshaShastri I actually saw that post before posting this, but had two problems. The selected answer is in Objective-C and also uses tableView. The most upvoted answer looks like it'll solve my problem (the one that solves it in a "pure collection view way"), but I'm not using storyboard. So, I'm struggling with their step 4 - how to register a second version of my cell and "store it as a property, so you can use it to determine the size of what the cell should be". Do you have any insight on how i can do this? – pozzy Feb 05 '19 at 06:08

1 Answers1

0

In your textViewDidChange method why are you setting height to infinity?

self.frame.size.height += estimatedSize.height

So, now you set the height of frame to infinity,that’s why cell is reaching bottom of the screen.

The cell height can be changed with constituting component’s height by using vertical or height constraint. Say I have 2 TextFields placed one on top of another. Then I can say, top of cell is 10 points away from top of first TextField and similarly bottom of cell is 10 points away from second TextField.

Now if I change the height of any TextField , height of cell will change.

Now for other solution, You if you are not using custom cell, then you will be returning UITableViewCell. You can try to add TextField or whatever component you want there and then return it. But be careful to reinitialize cell before use because dequeReusableCell will return used cells. So if that cell is already configured with a TextField and you want to add textview then you may want to use new one.