How to constrain to views inside UICollectionViewCell in iOS

Issue #422

To constrain views outside to elements inside UICollectionViewCell, we can use UILayoutGuide.

Need to make layout guide the same constraints as the real elements

1
2
3
4
5
6
let imageViewGuide = UILayoutGuide()
collectionView.addLayoutGuide(imageViewGuide)
NSLayoutConstraint.on([
imageViewGuide.topAnchor.constraint(equalTo: collectionView.topAnchor, constant: 16),
imageViewGuide.heightAnchor.constraint(equalTo: collectionView.heightAnchor, multiplier: 0.5)
])
1
2
3
4
NSLayoutConstraint.on([
loadingIndicator.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor),
loadingIndicator.centerYAnchor.constraint(equalTo: imageViewGuide.centerYAnchor)
])

Comments