UICollectionViewCell Padding

I am trying to set the padding to zero in the collection view cells, I set the "Min Interval" on the view controller:

enter image description here

However, it still has gaps between the cells:

enter image description here

I would also like the cells to be well tolerated depending on the frame width, for example, each cell has a width of 50 pixels, so if there are six cells, and I set the frame width to 150 pixels, it will display two lines of three cells.

However, if I set the frame width to 150 by doing:

- (void)viewDidLoad { [super viewDidLoad]; CGRect frame = self.collectionView.frame; frame.size.width = 150; self.collectionView.frame = frame; } 

It looks like in the above screenshot (too wide).

If I put it on something ridiculously small, such as 10, it will turn around to some extent:

enter image description here

UICollectionViewCell is set to 50 x 50:

enter image description here

I also tried setting the cell size programmatically, and also uninstalled UIEdgeInset:

 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0); } 

I turned off auto-linking only if you had any interference. Any tips on how I can remove the gasket and also wrap them depending on the width / height of the frame?

+8
objective-c uicollectionview uicollectionviewcell
source share
4 answers

You are using a UICollectionViewController, which, like the UITableViewController, has a collection view (or table view) as a base view property of the view controller. This means that it cannot be modified from within the view controller; its size is controlled by its supervisor - either a window, in this case, which has it as a root view controller and a parent view controller if you embed it.

If you want to reduce the viewing area of ​​collections so that the cells abut each other, you can change the insertion of sections in the form of a collection in the storyboard. In your case, an insert of 15 left and right combines the cells - this is 9 * 50 (450) plus 30 = 480, which is the width of the 3.5-inch iPhone in the landscape.

Obviously this will be different on the iPhone 5 or iPad. You can calculate insertions at runtime, use the collection view stored in the standard subclass of the UIViewController, or hold the UICollectionViewController as a built-in view controller. The last two let you simply specify the size, which is probably better than calculating the inserts.

+3
source share

I am not sure if these cells in the screenshot are 50x50 (EDIT: I think they are ...).

Make sure you include UICollectionViewDelegate and UICollectionViewDataSource.

This is the method you need to implement.

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize retval = CGSizeMake(50, 50); return retval; } 

If this does not work, try putting this code inside viewDidLoad

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setMinimumInteritemSpacing:0.0f]; [flowLayout setMinimumLineSpacing:0.0f]; [flowLayout setItemSize:CGSizeMake(50.0f, 50.0f)]; [self.collectionView setCollectionViewLayout:flowLayout]; 
+3
source share

Inject this method into your ViewController

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 2; //the spacing between cells is 2 px here } 
+3
source share

the minimum value InteritemSpacing is the minimum value, so it can be greater if the UICollectionViewLayout class decided that it should be

To fix this, you can create your own subclass layout and implement the method - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath , something like the answer from this question (see the second question, and not the one that is indicated as correct)

 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes* atts = [super layoutAttributesForItemAtIndexPath:indexPath]; if (indexPath.item == 0) // degenerate case 1, first item of section return atts; NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame; CGFloat rightPrev = fPrev.origin.x + fPrev.size.width + 10; if (atts.frame.origin.x <= rightPrev) // degenerate case 2, first item of line return atts; CGRect f = atts.frame; f.origin.x = rightPrev; atts.frame = f; return atts; } 
0
source share

All Articles