lithium
lithium

Reputation: 1302

Broken constraints in the cell

I need to show some UIView in my cell, but size of this UIView (backView) is different for each cell. So I need to change the height of this cell also.

I'm trying to change height and width of this backView in runtime, but all that I'm getting is a bunch of Autolayout errors like that:

[LayoutConstraints] Unable to simultaneously satisfy constraints. Will attempt to recover by breaking constraint...

I created SimpleTextCell class for this cell, and its init looks like that:

self.backView = UIView()
self.backView.translatesAutoresizingMaskIntoConstraints = false
self.backView.backgroundColor = UIColor.yellow
self.backView.layer.cornerRadius = 8.0
self.contentView.addSubview(backView)

let constraintBackViewTop = NSLayoutConstraint(item: backView, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .topMargin, multiplier: 1.0, constant: -5)
let constraintBackViewBottom = NSLayoutConstraint(item: backView, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottomMargin, multiplier: 1.0, constant: 5)
let constraintBackViewLeft = NSLayoutConstraint(item: backView, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .leftMargin, multiplier: 1.0, constant: 0)
constraintBackHeight = NSLayoutConstraint(item: backView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: superHeight)
constraintBackWidth = NSLayoutConstraint(item: backView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: superWidth)

self.contentView.addConstraints([constraintBackViewTop, constraintBackViewBottom, constraintBackViewLeft, constraintBackHeight, constraintBackWidth])

superHeight and superWidth are just some default values here.

my cellForRowAt function is like that:

let cell = tableView.dequeueReusableCell(withIdentifier: "simpleTextCell", for: indexPath) as! SimpleTextCell

cell.selectionStyle = .none
cell.backgroundColor = UIColor.red
cell.constraintBackHeight.constant = 100
cell.constraintBackWidth.constant = 200
cell.updateConstraints()

And it doesn't work at all. I'm having errors (like that I've mentioned earlier), and although the height of my cell is kinda right, my backView is invisible because width of backView.frame is zero (I don't have a clue why) and the height of backView is 100 (the same). It seems that my new constraints are ignored.

I've tried to set priority for these constraints in cellForRowAt:

cell.constraintBackHeight.priority = UILayoutPriority.init(rawValue: 999)

The result is run-time error.

I've tried to overcome this error by something like this:

cell.constraintBackHeight.isActive = false
cell.constraintBackHeight.priority = UILayoutPriority.init(rawValue: 999)
cell.constraintBackHeight.isActive = true

but it doesn't work either.

How can I fix this?

Upvotes: 0

Views: 57

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

The problem is here

 constraintBackHeight = NSLayoutConstraint(item: backView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: superHeight)
 constraintBackWidth = NSLayoutConstraint(item: backView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: superWidth)

you're setting 2 height constraints , it may be you just copied to make as a width and forget to change it to .width

Upvotes: 1

Related Questions