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() } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With