Reputation: 8049
I need 2 cells for each row.
With exactly the middle of the controller width.
In iPhone 5 case. The Screen width is 320
and each row will be 160
.
self.view.frame.size.width => 320.0
midleWidth => 160.0
But when the CollectionView is drawed the cell are separated, like the next image:
My CollectionView config is:
When i put the
self.view.frame.size.width
as the width of cell, take all width of the screen as expected.
Upvotes: 0
Views: 2601
Reputation: 958
Try this Below Code
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
}
Upvotes: 3
Reputation: 914
The above answer won't work for all devices try this
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size;
return CGSizeMake(windowSize.width/2,heightOfCell);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.0
}
Upvotes: 1
Reputation: 1067
// use like this it will work for all devices
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size;
CGRect bounds = [[UIScreen mainScreen] bounds];
double bWidth = (bounds.size.width/320)* 200 ; // 200 is your cell width which given in xib
double bheight = (bWidth/320)* 250 // 250 is your cell height which given in xib;
size = CGSizeMake(bWidth, bheight);
return size;
}
Upvotes: 1