試試這個下面的代碼
工作
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
"Cell", forIndexPath: indexPath)
let viewsDictionary = ["View1": collectionView]
collectionView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[View1]|", options: [], metrics: nil, views: viewsDictionary))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[View1]|", options: [], metrics: nil, views: viewsDictionary))
return cell
}
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
let cellSize:CGSize = CGSizeMake(self.bounds.width/2, self.bounds.height)
return cellSize
}
已在代碼中設置單元格間距任何地方? –