UICollectionView layout issue on iphone 5 - ios

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)

Related

How to remove space between cells in UICollectionView? [duplicate]

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
}

iOS UICollectionViewCell

I am trying to make the spacing between my cells = 5
In the iPhone 4s the UICollectionViewCell becomes a square but I need rectangle.
I need iPhone4s iPhone5 iPhone6 iPhone 6Plus the cell, but the width and height of the cell is always wrong. I don't know how to do it automatically.
This is my code:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 20) /2, ([UIScreen mainScreen].bounds.size.height) /3);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
What am I missing? Thanks in advance!
some change in your code will works fine
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int itemWidth=([UIScreen mainScreen].bounds.size.width - 20)/2;
int intemHeight=itemWidth*2/3;
return CGSizeMake(itemWidth, intemHeight);
}

UICollectionViewCell with flexible width

I have a basic collection view with vertical scrolling, sections and variable amount of cells in each section. I need to adjust the width of each cell to fill available width of the view up to 4 cells, when I need to start next row of cells.
If a section has 1, 2, 3 or 4 cells, their width should be 1/1, 1/2, 1/3 or 1/4 of the view's width respectively. Sections with more then 4 cells should stretch 4 cells in one row and continue on the other row.
Do I need to subclass UICollectionViewFlowLayout for this or can I handle it in UICollectionViewController class and how?
You should work with below two methods to change the column in one row
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5,5,0,5);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(75, 75);
}
You have to play with UICollection view Delegate methods to give a custom design for CollectionCell
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0,5,0,5);
}
and size you can set in Frame File inspector

Issues laying UICollectionView to create 2x3 grid iOS

I am attempting to lay my UICollectionView out such that it is a 2x3 grid per page. Here is what I am getting so far:
As you can see, the next "page" is peeping in from the right, which I don't want. I figured if I know my width is always going to be 320pts (iPhone only), then I would want my cells to be 130pts in width with 15pts on either side.
Code for layout delegate:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(130, 130);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20.0f, 15.0f, 20.0f, 15.0f);
}
And also cellForItemAtIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustCollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:kReuseID
forIndexPath:indexPath];
//Set up list name label
cell.LblListName.font = [self getDefaultFontWithSize:15.0f];
cell.LblListName.textColor = fontAndTertiaryColor;
cell.LblListName.textAlignment = NSTextAlignmentCenter;
cell.LblListName.text = #"Failtron";
//cell.LblListName.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = secondaryColor;
cell.parallaxIntensity = kParallaxMotion;
[Utilities createViewBorder:cell];
return cell;
}
I thought an inset would act like padding in CSS but I believe I'm off the mark with that one, I should also note that in the UIStoryboard my layout is set to flow. Please let me know if I am missing any relevant code, this is my first attempt with a UICollectionView. I would like this to scroll horizontally as well.
Ok, there are tons of ways to manipulate layouts for a UICollectionView, but sounds like since you are using "flow" you can just implement <UICollectionViewDelegateFlowLayout> in your .h and then use the following methods to tweak your layout to your liking:
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// Size of the cell
CGSize retval = CGSizeMake(130,130);
return retval;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
//top, left, bottom, right
return UIEdgeInsetsMake(0, 20, 0, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//Minimum spacing between cells
CGFloat interimSpacing = 0.0f;
return interimSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
// Minimum spacing between lines. In your case,
this is the distance between columns
// If your scroll direction was vertical,
this would be the distance between rows
CGFloat lineSpacing = 20.0f;
return lineSpacing;
}
I've prepopulated this code with some values that should be pretty darn close to what you wanted to accomplish.
Alternatively, all of this can be done in Interface Builder by selecting the UICollectionView and adjusting the values in the Size Inspector, albeit more confusing in my opinion.

UICollectionView- How to remove spacing above first cell?

UICollectionView- How to remove spacing above first cell?
I have the top inset = 0, and lines = 10
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return CGSizeMake(collectionView.width, 44);
}
return CGSizeMake(300, 150);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 10, 10, 10);
}
Seems it is something to do with setting the first cell to a different size
if i remove the check for row ==0 in sizeForItemAtIndexPath and return same for all cells it is flush with the top.
Any ideas?
Solution is just to write inside viewDidLoad
self.automaticallyAdjustsScrollViewInsets = NO;
I did it following way (tested on iOS7, 55 is a magic value):
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
//-55 is a tweak value to remove top spacing
return CGSizeMake(1, -55);
}
Alternative way using your code:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(-55, 10, 10, 10);
}
If you'd like to use Interface Builder, you can uncheck "Adjust Scroll View Insets" on the View Controller's Attribute Inspector:
Or use Amit's answer to do it programatically.
I don't think so that one is your top inset. Edit your question with some piece of code you have applied.
If it was collectionView inset then it should remove by passing 0 as top inset.
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 10, 10, 10);
}
And the following layout delegate used for row spacing.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 30;
}
Using srtuct and spring set the collectionview autoresizing mask like this and also make sure you set the frame y to 0

Resources