I am working on collection-view Based project on which Numberofitem will be changed and increase according to user's click on Particular Index. all Works perfectly in my project but i am not able to set Cell size according to number of cell's in collection-view.Moreover collectionview will not be scrollable all cell fix in view with resize. Here i have attached all the image's .
1) Storyboard View
2) Result With Four Cell's
3)when Number of Cell Increase
Currently i done setup of my cell by this way with following code. but i want to set that, if there is four cell in collection-view then the size of each cell increases and fit according to collection-view and device size (iphone 5s , iphone 6 , iphone 6plus)
Here is my Try,
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 5.0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 5.0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
////@property NSInteger count;////
if (_count == 0) {
// width = ((width - (span*5))/4);
// return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/4);
return CGSizeMake(self.view.frame.size.width/2.5, self.view.frame.size.height/5);
}
if (_count == 1 || _count == 2)
{
return CGSizeMake(self.view.frame.size.width/2.5, self.view.frame.size.height/5);
// return CGSizeMake(100.0, 100.0);
}
if ( _count == 3 || _count == 4 || _count == 5)
{
// return CGSizeMake(100.0, 100.0);
return CGSizeMake(self.view.frame.size.width/3.5, self.view.frame.size.height/6.5);
}
if ( _count == 6 || _count == 7 || _count == 8 || _count == 9)
{
//return CGSizeMake(90.0, 90.0);
return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/8);
}
if ( _count == 10 || _count == 11 || _count == 12 || _count == 13 || _count == 14)
{
// return CGSizeMake(80.0, 80.0);
return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/8);
}
if ( _count == 15 || _count == 16 || _count == 17 || _count == 18 || _count == 19 || _count ==20)
{
//return CGSizeMake(70.0, 70.0);
return CGSizeMake(self.view.frame.size.width/4.75, self.view.frame.size.height/9.5);
}
if (_count ==21 || _count == 22 || _count == 23 || _count == 24 || _count == 25 || _count == 26 || _count == 27) {
// return CGSizeMake(60.0, 60.0);
return CGSizeMake(self.view.frame.size.width/6, self.view.frame.size.height/12);
}
if (_count ==28 || _count == 29 ) {
// return CGSizeMake(50.0, 50.0);
return CGSizeMake(self.view.frame.size.width/6.25, self.view.frame.size.height/12.5);
}
else
return CGSizeMake(0, 0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return itemincollectionview.count;
}
Note:- I have also tried this all Method.
- (void)viewDidLayoutSubviews
{
self.automaticallyAdjustsScrollViewInsets = NO;
[self.collectionView layoutIfNeeded];
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:_setRandomIndex inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.collectionView.collectionViewLayout invalidateLayout];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.view layoutIfNeeded];
}