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
?