My Notification Center widget will need to have a dynamic height based on the content it contains. I have a simple interface - a single UILabel
with a UICollectionView
underneath. (The collection view will grow in height based on the size I provide for the cells in the flow layout.)
What constraints are necessary in order to properly specify the height of the widget?
I thought it would be enough to provide a Top constraint on the label to fix it to the view's Top, specify the collection view's Top aligns with the label's Bottom, then provide a fixed Height for the collection view (which is updated when the itemSize
changes), then align the collection view's Bottom to the view's Bottom. But this results in two broken constraints - collection view's height and the vertical constraint between the label and the collection view.
let label = //...
label.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(label)
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 10))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: 25))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .LeadingMargin, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .TrailingMargin, multiplier: 1, constant: 0))
let collectionView = //...
collectionView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(collectionView)
self.view.addConstraint(NSLayoutConstraint(item: collectionView, attribute: .Leading, relatedBy: .Equal, toItem: label, attribute: .Leading, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: collectionView, attribute: .Trailing, relatedBy: .Equal, toItem: label, attribute: .Trailing, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: collectionView, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: collectionView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0))
self.collectionViewHeightConstraint = NSLayoutConstraint(item: collectionView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: 100)
collectionView.addConstraint(self.collectionViewHeightConstraint)
//later on
let flowLayout = collectionView.collectionViewLayout as UICollectionViewFlowLayout
flowLayout.itemSize = //some new size
self.collectionViewHeightConstraint.constant = flowLayout.itemSize.height * numberOfRows
The problem:
Unable to simultaneously satisfy constraints.
"<NSLayoutConstraint:0x6080000998c0 V:|-(10)-[UILabel:0x6000001db300] (Names: '|':UIView:0x60800018f700 )>",
"<NSLayoutConstraint:0x608000099e60 V:[UILabel:0x6000001db300(25)]>",
"<NSLayoutConstraint:0x60800009a2c0 V:[UILabel:0x6000001db300]-(0)-[UICollectionView:0x7ff94b02c200]>",
"<NSLayoutConstraint:0x60800009a310 UICollectionView:0x7ff94b02c200.bottom == UIView:0x60800018f700.bottom>",
"<NSLayoutConstraint:0x60800009a360 V:[UICollectionView:0x7ff94b02c200(100)]>",
"<NSLayoutConstraint:0x60800009a4f0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x60800018f700(667)]>"
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60800009a360 V:[UICollectionView:0x7ff94b02c200(100)]>
And
Unable to simultaneously satisfy constraints.
"<NSLayoutConstraint:0x6080000998c0 V:|-(10)-[UILabel:0x6000001db300] (Names: '|':UIView:0x60800018f700 )>",
"<NSLayoutConstraint:0x608000099e60 V:[UILabel:0x6000001db300(25)]>",
"<NSLayoutConstraint:0x60800009a2c0 V:[UILabel:0x6000001db300]-(0)-[UICollectionView:0x7ff94b02c200]>",
"<NSLayoutConstraint:0x60800009a310 UICollectionView:0x7ff94b02c200.bottom == UIView:0x60800018f700.bottom>",
"<NSLayoutConstraint:0x60800009a4f0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x60800018f700(0)]>"
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60800009a2c0 V:[UILabel:0x6000001db300]-(0)-[UICollectionView:0x7ff94b02c200]>