Well, this pins my UIImageView
to the Top of the screen... underneath the nav and status bars. Shouldn't be any different with a collection view:
NSLayoutConstraint *top = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
Edit: Sorry, was working in an Obj-C project... here it is in Swift:
let img = UIImage(named: "Man")
let imageView = UIImageView(image: img)
imageView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(imageView)
imageView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 200.0).isActive = true
imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

and... here it is with a collection view also (kinda hard to tell with a plain screen-cap):
self.view.backgroundColor = .white
let img = UIImage(named: "Man")
let imageView = UIImageView(image: img)
imageView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(imageView)
imageView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 200.0).isActive = true
imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "defaultCell")
cv.dataSource = self
cv.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(cv)
cv.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.9).isActive = true
cv.heightAnchor.constraint(equalToConstant: 300.0).isActive = true
cv.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
cv.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
