I have implemeted this method
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
So the method invokes in any case because if I change for example with UIEdgeInsetsMake(0, 0, 100, 0) I can see 100 pt padding on the top of each section. But if I set zero value there is still approximately 10 pt between lines.
Set that in your flow layout
UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
flow.scrollDirection = UICollectionViewScrollDirectionVertical;
flow.minimumInterItemSpacing = 0; // space between cells in row
flow.minimumLineSpacing = 0; // space between rows
yourCollectionView = [[UICollectionView alloc]initWithFrame:someFrame collectionViewLayout:flow];
Or Delegate Method:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
// space between cells on different lines
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
// space beetween cells in same row
}
Related
This question already has answers here:
Left Align Cells in UICollectionView
(24 answers)
Closed 5 years ago.
How to remove space between two cells in UiCollectionView? Min spacing is not working.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
return CGSizeMake((_collView2.frame.size.width/3)-20,300);
}
}
But this is not working.
*
*
Set Min Spacing for cells and for Lines to Zero
and replace your code by
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
return CGSizeMake((_collView2.frame.size.width/3),300);
}
}
Use UICollectionViewFlowLayout for those purposes:
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
This happens because of how UICollectionviewFlowLayout works. You could provide a custom collectionview layout:
Left Align Cells in UICollectionView
Or use a 3d party solution:
https://github.com/mokagio/UICollectionViewLeftAlignedLayout
use this code, based on your design adjust the width and height.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath {
CGSize s = CGSizeMake([[UIScreen mainScreen] bounds].size.width/2-10,
[[UIScreen mainScreen] bounds].size.height/2.9-7);
return s;
}
bellow code adjust the edges of collection view
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(2, 6,2, 6);
}
Use both delegates and change the width and height
A method called minimumInteritemSpacingForSectionAtIndex from UICollectionViewFlowLayoutDelegate could help you. The minimunIteritemSpace set the space beetween items horizontally, for example try this code:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0; // This is the minimum inter item spacing, can be more
}
Also you can use the edgeInsets to change the space beetween items.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(20,20,20,20); // top, left, bottom, right
}
I was trying to remove the space between UICollectionView and this is the screenshots from device
I am really trying to avoid those spaces(blue color) and i have tried following codes
This is my collectionview delegate
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
First I tried this one, and its not working
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cell.frame.size.width, cell.frame.size.height);
flow.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
}
and I tried this one also, its not working
#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 5.0;
}
Please help me to fix this.
So there is properties and methods can limit the minimum space, such as minimumInteritemSpacingForSectionAtIndex
However it is just the 'minimum space' not always the max space.
This what I am using: borrow from Cell spacing in UICollectionView
Override standard flow layout:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [super layoutAttributesForElementsInRect:rect];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 4;
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
maximumSpacing could be set to any value you prefer
From storyboard also you can set the spacing attributes. Select your collection view - select size inspector tab, there you can adjust minimum spacing for for cells or for lines.
And you can adjust section insets also.
flow.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
Use sectionInset for your UICollectionViewFlowLayout in viewdidload.
See my answer on how to adjust UICollectionView width (using AutoLayout constraint) to fit all cells perfectly without any spacing between them.
I have a problem with my UICollectionView, I have 3 columns and everything works fine instead of iphone 5 and 4.
In iphone 6 my collectionview looks that (width in cell):
Iphone 5 with cells:
The problem is, when in last row, I have 2 cells, and it looks like that:
The look I want to achieve:
Every where I can, I set insets to (0, 0, 0, 0). I use default layout with my insets set in storyboard. So is it possible to achieve this with flow layout or I need to look into custom layout?
=== edit
I create UICollectionView in storyboard and implements delegate:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
float width = self.frame.size.width / 3.0f;
float height = floorf(width + 118);
if(SMALLER_THAN_6){
width = floorf(width);
if([indexPath row] % 3 < 2){
++width;
}
}
return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
I solved this problem by inserting an empty cell at the end of the row (only on iphone 4 & 5)
I got a UICollectionView, my setup:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
if (collectionView == self.navigationCollectionView)
return 24;
else
return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if (collectionView == self.navigationCollectionView)
return UIEdgeInsetsMake(14.5, 15, 0, 15);
else // chart collection
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView == self.navigationCollectionView) {
DashboardModel *dashModel = [self getDashBoardCacheAtIndex:indexPath.row];
UILabel *label = [[UILabel alloc] init];
label.text = dashModel.dashboardTitle;
label.font = [UIFont boldSystemFontOfSize:17];
[label sizeToFit];
return CGSizeMake(label.bounds.size.width,self.navigationCollectionView.bounds.size.height);
}
}
But when running, it shows like this:
First cell frame is (39,0,85,44)
Second cell frame is (148,14.5,50,44)
The first cell's top inset is not correctly set. What's wrong with it?
EDIT:
I feel the cell is auto-centered by UICollectionView. Can we set not to auto-center?
I have a problem with iOS UICollectionView.
I have a UICollectionView with a layout:
UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewFlowLayout.itemSize = CGSizeMake(COLLECTION_VIEW_CELL_WIDTH, COLLECTION_VIEW_CELL_HEIGHT);
collectionViewFlowLayout.minimumLineSpacing = 3.0f;
My UICollectionView frame is frame = (0 ,45, 769,307);
Also, I have a custom UICollectionViewCell with frame frame = (0 ,0, 190, 100)
190 * 4 = 760. That means, that 4 cells should fit into my collection view width. But there is only 3 of them...
Where is my problem?
you can try this delegate
#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}