Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCell Padding

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

enter image description here

Yet it still has gaps between the cells:

enter image description here

Also I'd like it so that the cells wrap nicely depending on the width of the frame, for example each cell is 50px wide, so if there are six cells and I set the frame width to 150px, it will display two lines of three cells.

Yet 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 screen shot (too wide).

If I set it to something ridiculously small such as 10, it then wraps to some extent:

enter image description here

The UICollectionViewCell is set to 50 x 50:

enter image description here

I have also tried setting the size of the cell programatically, and also removed the UIEdgeInset:

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

I have disabled auto layout just incase that had any interference. Any advice as to how I can remove the padding and also have them wrap depending on the frame width / height?

like image 268
StuR Avatar asked Jan 03 '13 18:01

StuR


People also ask

How CollectionView works?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.

What is a CollectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content. Layouts.

How do I change the spacing between cells in collection view?

I have found very easy way to configure spacing between cells or rows by using IB. Just select UICollectionView from storyboard/Xib file and click in Size Inspector as specified in below image. For configuring space programatically use following properties. 1) For setting space between rows.

How to set Collection view in swift?

Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices. The foundation is now set up in the storyboard.


3 Answers

You are using a UICollectionViewController which, like a UITableViewController, has the collection view (or table view) as the base view property of the view controller. This means it can't be resized from within the view controller; it's size is controlled by its superview - either the window, in this case, which has it as the root view controller, or a parent view controller if you were embedding it.

If you want to reduce the collection view area so the cells abut one another, you can amend the section insets of the collection view in the storyboard. In your case, an inset of 15 left and right brings the cells together - this is 9 * 50 (450) plus 30 = 480 which is the width of a 3.5inch iPhone in landscape.

Obviously this will be different in the iPhone 5 or iPad. You can either calculate the insets at run time, use a collection view held in a standard UIViewController subclass, or hold the UICollectionViewController as an embedded view controller. The latter two will enable you just to specify a size, which is probably nicer than calculating insets.

like image 74
jrturton Avatar answered Nov 08 '22 18:11

jrturton


I am not sure those cells in the screenshot are 50x50 (EDIT: I guess they are...).

Check if you connected the UICollectionViewDelegate and UICollectionViewDataSource.

This it the method you need to implement

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

If it 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];
like image 40
Nikola Kirev Avatar answered Nov 08 '22 16:11

Nikola Kirev


Implement this method in your ViewController

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
   return 2; //the spacing between cells is 2 px here
}
like image 41
ahmad Avatar answered Nov 08 '22 17:11

ahmad