iOS - UICollectionView still have spacing between cells after set to 0 - ios

Im getting small spaces between cells when setting everything to 0. All I did in the cellForItemAtIndexPath is setting backgroundColor for each cell.
Here's my code:
//collectionView frame
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) collectionViewLayout:layout];
...
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//The cell sizes are divided by the width of the collectionView.
return CGSizeMake(_collectionView.frame.size.width/12, _collectionView.frame.size.width/12);
}
- (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.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0f;
}
My collection view looks like this. You'll see that there are black lines between some cells. What is causing this? How can I remove the lines?
Any help is greatly appreciated. Thanks in advance.

I too faced this problem. Thanks to #tenric for pointing out and yes for me was because the width is not divisible by the number of desired columns. My solution as follow:
var itemWidth: CGFloat {
get {
return floor(view.frame.size.width / 7)
}
}
And because I'm using AutoLayout, I just adjust the gap to the width of the collectionView as follow (refer to widthAnchor):
collectionView?.topAnchor.constraint(equalTo: headerSeparator.bottomAnchor).isActive = true
collectionView?.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
collectionView?.bottomAnchor.constraint(equalTo: footerSeparator.topAnchor).isActive = true
let gap = view.frame.size.width - (itemWidth * 7)
collectionViewWidthConstraint = collectionView?.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -gap)

because both width and height are not divisible by 12,so it would remain some empty pixel point.To avoid this,both width and height of collectionView should be multiples of 12.

I was going crazy over this too. I wanted a generic solution since I had 3 cells per row for iPhones and 4 cells per row for iPads and the UICollectionView was covering the entire width of the screen and the width is different between all devices.
I ended up making the combined width of the cells on a row wider than the device screen by using ceil(frame.width / itemsPerRow). I then calculated the total width of the cells on a row and compared with the screen width (view.frame) to get the width overflow the cells would create. Knowing the overflow I set the trailing constraint of the UICollectionView to be negative the overflow. This means the UICollectionView is actually going to overflow the screen width with a pixel or so, but in my case this was not an issue. Hopefully the code below will help clarify and give you an idea of how this could be solved.
// calculating cell width
let frame = view.frame
let cellWidth = ceil(frame.width / itemsPerRow)
// adjust UICollectionView to avoid space between cells
let totalCellsWidth = cellWidth * itemsPerRow
let overflow = totalCellsWidth - frame.width
let adjustedConstraint = -overflow
collectionViewsTrailingConstraint.constant = adjustedConstraint

Related

In my app I used uicollectionview then how to make difference between two cell same in all iPhone models?

In my app, I used UIcollectionview then how to make difference between two cell same in all iPhone models. and auto layout is turned on I tried many things but the issue is not resolved my app looks perfect on iPhone 5s but It shows more difference between two cells in iPhone 6s and 6s plus.enter image description here
You need to calculate the cell dynamically according to the screen size. I have faced such situation and fixed it. You can try this, might be help you...
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(width, height);
}
get width and height
-(void)getDynamicCellSize {
width = (SCREEN_SIZE.width - 50)/3;
height = (SCREEN_SIZE.height - (135+self.tabBarController.tabBar.frame.size.height)-29)/3;
} // 135 means the navigation bar height + segment control (I have in my design)
CGFloat width, height;// Declare globally
Call getDynamicCellSize method in viewWillAppear or didLoad
OR
Use this code in sizeForItemAtIndexPath method in 'SWIFT'
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing) / 2
return CGSize(width: cellWidth, height: cellWidth)
Happy coding...

Collection View vertical padding issue

I am making a collection view which is horizontally scrolled. The collection view is in a uitableview cell
My height of collection view is 170
I am making a cell of 75x75
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
return UIEdgeInsetsMake(4, 5, 6, 3);
}
This is my inset what I setting. Padding from top is 4 left is 5 right is 3 and bottom is 6. The collection view has 20 items horizontally scrolled. Now my problem is the vertical spacing of items when two rows are made. I want to keep it to minimum. I used this
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 2.5;
}
It provided a 2.5 padding between various items but still my vertical padding is too much as you can see from the image that vertical space between 2 lines of images is around 10. I just want to decrease that to 2.5. I did some change in return UIEdgeInset but if i increase padding from bottom, then the collection view only makes a single row that i dont want. for example if i use this
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(4, 5, 7, 3);
}
I get this output
How to overcome this problem. I just want equal and minimum spacing between all my items. which are a square (75x75). Any help will be appreciated. Thanks
Use the following code to set the height of the collection view cell.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
var collectionViewHeight = collectionView.fram.size.height
let cellheight = collectionViewHeight/2 - 5
return CGSize(width: cell height , height: cellheight)
}
now what it will do is that it will calculate the height of the collection view and divide it by 2 if you have fix 2 number of rows and and subtract 5 from each
as collectionViewheight is 170 and divide by 2 is 85 and then - 5 is 80 height for every cell and with width equal for square cell
this will perfectly fit the view
else if you can change the values according to you requirement
Minimum line spacing is just that -- it set the minimum spacing, but doesn't do anything to set the maximum. The collection view will add extra space between the rows if it needs to based on the size of the collection view. Your math doesn't add up. 2 X 75 (cell height) + 4 (top) + 6 (bottom) + 2.5 (interline spacing) = 162.5, but your collection view's height is 170. Change the collection view's height to 162.5, and it should work.

Cell spacing in UICollectionView minimumInteritemSpacing

How to do set cell spacing in a section of UICollectionView? UICollectionView there is a property minimumInteritemSpacing, I need to set 1.0 still it not work. And I implemented the delegate method.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}
To check about how your collection Cells would look, you can try this in your Storyboard with the help of storyboard. First just for the sake of checking, put some static cells in your CollectionViewController like this so that your screen appears like this :
No you can see those cells and the spacing between them. In your case, the cells will appear with improper spacing as you have shown above. So now open with this screen open, open up the Size Inspector fron your Interface Builder. It would look something like this :
Now you can see some options in the size inspector window. You can adjust the size of each cell and also the spacing between them using the Min. Spacing option. And finally for equal spacing from left and right sides, use the Section Insets Option. As you change the values there, the change will be reflected in your Controller View. So you can get an idea if you want to increase/decrease some values.
Hope this helps.
you can set programmatically like this
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.minimumLineSpacing = 0.0;
layout.minimumInteritemSpacing = 0.0;
contestsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
This method set up spaces between cells, footer and header:
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 20, 20, 20);
}

Space between UICollectionViewCell rows

Currently I have a 9 items displayed in my collection. I want a 3*3 grid for each section I have. So when I want to go to the next section I scroll to the right and and I see the 3*3 grid for my next section. What I did to accomplish this was setting the height of the UICollection to 3*height_of_my_cell. I also made sure that:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0f;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
So the goal is really to have the cells pasted to each other (no space between them). The problem is that what I am getting is this:
To see the 7,8 and 9 I need to scroll to the right. So I see that I am able to get rid of the space between the "columns" but not the rows. So do I need o create a custom UICollectionViewFlowLayout to achieve this? Also this is being done on iOS7, which I am not sure if it might influence the final result.
Just set the property of UICollectionViewFlowLayout, such as:
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
Was able to solve the issue, by creating a custom UICollectionViewFlowLayout and adding this line:
[self setSectionInset:UIEdgeInsetsMake(-65.0f, 1.0f, 0.0f, 0.0f)];

UICollectionView With Variable Cells Displaying Them Weirdly

I have a UICollectionView that I want to have cells that are always half the height of the display and sometimes half of the width but sometimes 1/4th the width. In the picture below I have drawn what the UICollectionView is displaying. In landscape I am telling it to display a 1/4th width cell and then a half cell and then another 1/4th and that repeats again. In theory there should be 6 cells on the screen, but the cells end up all scattered. I can't figure out what I'm doing wrong. If I have every cell one size it works fine, just with different sizes this happens.
This is what happens:
This is what I want to happen:
Heres my code for setting up the layout and altering the individual cell sizes:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([bigCells containsObject: indexPath.row]) {
return CGSizeMake((CollectionView.frame.size.width/2), (CollectionView.frame.size.height/2));
} else {
return CGSizeMake((CollectionView.frame.size.width/4), (CollectionView.frame.size.height/2));
}
}
- (void)prepareLayout {
self.itemSize = CGSizeMake((self.collectionView.frame.size.width/2), (self.collectionView.frame.size.height/2));
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
}

Resources