Tobias Grunwald
Tobias Grunwald

Reputation: 189

Update Height Constraint in a UICollectionViewCell

I have a Facebook like blog post View Controller with a headline, an optional image and a text.

I am trying to update the height constraint of the image so that there is no space between the headline and the text, like this:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "blogPostCell", for: indexPath) as? BlogPostCell else{
        fatalError("Not an instance of BlogPostCell")
    }
    cell.headlineLabel.text = blogPosts[indexPath.row].headline
    cell.postTextLabel.text = blogPosts[indexPath.row].content

    cell.postImage.translatesAutoresizingMaskIntoConstraints = false
    let cellHeigtConstraint = cell.postImage.heightAnchor.constraint(equalToConstant: 0)

    if let img = blogPosts[indexPath.row].image{
        cell.postImage.contentMode = .scaleAspectFit
        cellHeigtConstraint.isActive = false
        cellHeigtConstraint.constant = self.view.frame.width*0.75
        cellHeigtConstraint.isActive = true
        cell.postImage.kf.indicatorType = .activity
        let url = URL(string: App.rootdir + "/assets/uploads/" + img)
        cell.postImage.kf.setImage(with: url)
    } else{
        cellHeigtConstraint.isActive = false
        cellHeigtConstraint.constant = 0
        cellHeigtConstraint.isActive = true
    }
    return cell
}

So I am trying to deactivate the constraint, then change it and then activate it again. But instead of an updated constraint I seem to have two constraints:

2018-01-15 14:20:44.317167+0100 docprice[86466:2062563] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60800009cc00 UIImageView:0x7f80dd53e280.height == 0   (active)>",
    "<NSLayoutConstraint:0x60c000289970 UIImageView:0x7f80dd53e280.height == 310.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000289970 UIImageView:0x7f80dd53e280.height == 310.5   (active)>

I guess because of the dequeued cells I can't address the constraint like this. But I don't see another way to implement this.

Upvotes: 2

Views: 1947

Answers (1)

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

First, you should do something like this:

if let img = blogPosts[indexPath.row].image{
        cell.postImage.contentMode = .scaleAspectFit
        cellHeigtConstraint.constant = self.view.frame.width*0.75
        cell.postImage.kf.indicatorType = .activity
        let url = URL(string: App.rootdir + "/assets/uploads/" + img)
        cell.postImage.kf.setImage(with: url)
    } else{
        cellHeigtConstraint.constant = 0
    }

cell.layoutIfNeeded()

Then I suggest that every cell has its own cellHeigtConstraint and it's not one for all the cells. It should be a property inside yout BlogPostCell class.

So in the end you should have cell.cellHeigtConstraint.constant = 0

Upvotes: 2

Related Questions