1

I tried to create a UICollectionView with one of the following methods:

// method 1
collectionView = UICollectionView(frame: CGRectMake(0, 0, 500, 3000), collectionViewLayout: layout)  

// method2
collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)  

// method 3
collectionView = UICollectionView()

And then set auto layout constraints for it as below:

mainView.addSubView(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leadingAnchor.constraintEqualToAnchor(mainView.leadingAnchor).active = true
collectionView.trailingAnchor.constraintEqualToAnchor(mainView.trailingAnchor).active = true
collectionView.topAnchor.constraintEqualToAnchor(mainView.topAnchor, constant: 50).active = true
collectionView.heightAnchor.constraintEqualToConstant(300).active = true

With method 1, the UICollectionView still shows by following the initial frame setup (0, 0, 500, 3000)

With method 2, the UICollectionView does not show up.

With method 3, it will crash with reason: "UICollectionView must be initialized with a non-nil layout parameter"

What's the correct way to set auto layout constraints for this UiCollectionView?

Joe Huang
  • 6,296
  • 7
  • 48
  • 81
  • Hope so this may help you http://stackoverflow.com/a/26181982/2594560 – Ankita Shah Jun 03 '16 at 09:30
  • 1
    I have been using auto layouts in many places and they are all good. Just for `UICollectionView`, I don't know how to do it. Your link does not help, thanks. – Joe Huang Jun 03 '16 at 09:35

0 Answers0