Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS "UITemporaryLayoutHeight" constraint

Tags:

I am using systemLayoutSizeFittingSize to get the smallest size for a custom view subclass that satisfies its internal constraints.

let someView = SomeView()         someView.setNeedsUpdateConstraints() someView.setNeedsLayout() someView.layoutIfNeeded()  let viewSize = someView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)  println(viewSize) // PRINTS (0.0, 96.0) WHICH IS RIGHT! 

I get the correct value but I also get a Unable to simultaneously satisfy constraints warning:

"<NSLayoutConstraint:0x7ff16b753190 '_UITemporaryLayoutHeight' V:[ProjectName.SomeView:0x7ff16b772210(0)]>", "<NSLayoutConstraint:0x7ff16b75de50 UIImageView:0x7ff16b76eff0.top == ProjectName.SomeView:0x7ff16b772210.topMargin>", "<NSLayoutConstraint:0x7ff16b7653f0 UIImageView:0x7ff16b76eff0.bottom <= ProjectName.SomeView:0x7ff16b772210.bottomMargin>" 

Below is my SomeView UIView subclass.

It just contains an 80x80 imageView pinned to the top, left and bottom margins. (I am using PureLayout to write constraints).

Now obviously this view will have a fixed height of 96 (80 + 8x2 for margins) but theoretically it could stretch if its subviews changed size.

Any ideas? Searching google for UITemporaryLayoutHeight (or Width) gives 0 results...

class SomeView: UIView {      let imageView = UIImageView()      var constraintsSet = false      override init(frame: CGRect) {          super.init(frame: frame)          backgroundColor = UIColor.lightGrayColor()          imageView.backgroundColor = UIColor.darkGrayColor()         addSubview(imageView)     }      override func updateConstraints() {          if(!constraintsSet) {              imageView.autoPinEdgeToSuperviewMargin(.Top)             imageView.autoPinEdgeToSuperviewMargin(.Left)             imageView.autoPinEdgeToSuperviewMargin(.Bottom, relation: NSLayoutRelation.GreaterThanOrEqual)             imageView.autoSetDimension(.Width, toSize: 80.0)             imageView.autoSetDimension(.Height, toSize: 80.0)              constraintsSet = true         }         super.updateConstraints()     } } 
like image 658
LorTush Avatar asked May 07 '15 16:05

LorTush


1 Answers

I've noticed this happening to me if I call layoutIfNeeded on a view before its parent has been laid out. I would bet if you removed someView.layoutIfNeeded() you wouldn't see that error anymore. You could probably also get rid of someView.setNeedsLayout() and replace it with someView.updateConstraintsIfNeeded(). systemLayoutSizeFittingSize shouldn't require the views be actually laid out, it just needs the constraints to be configured properly.

like image 172
Stakenborg Avatar answered Sep 22 '22 08:09

Stakenborg