4

There are one to three UICollectionViewCells in a UICollectionView. Is there a way to always position the cell(s) at the bottom of screen after reloadData?

+----------------+     +----------------+     +----------------+
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     | +------------+ |
|                |     |                |     | |   cell 1   | |
|                |     |                |     | +------------+ |
|                |     | +------------+ |     | +------------+ |
|                |     | |   cell 1   | |     | |   cell 2   | |
|                |     | +------------+ |     | +------------+ |
| +------------+ |     | +------------+ |     | +------------+ |
| |   cell 1   | |     | |   cell 2   | |     | |   cell 3   | |
| +------------+ |     | +------------+ |     | +------------+ |
+----------------+     +----------------+     +----------------+
Sameer Donga
  • 948
  • 9
  • 24

1 Answers1

1

Just call following method after loading the collection view and you will get what you need.

 - (void)updateContentInset {
        NSInteger numRows = [self collectionView:collectionView numberOfItemsInSection:0];
        CGSize contentInsetTopSize = collectionView.bounds.size;
        CGFloat contentInsetTop = contentInsetTopSize.height;
        int i=0;
        for (i=0;i<numRows;i++) {
            CGFloat height = 70 + [self heightOfTextForString:[arrNotes objectAtIndex:[NSIndexPath indexPathForItem:i inSection:0].row] andFont:[UIFont fontWithName:@"HelveticaNeue-LightItalic" size:13.0] maxSize:CGSizeMake(157, FLT_MAX)];
            contentInsetTop = contentInsetTop - height;

            if (contentInsetTop <=0) {
                contentInsetTop = 0;
                break;
            }
        }

        NSLog(@"Height-->%f",contentInsetTop);
        collectionView.contentInset = UIEdgeInsetsMake(contentInsetTop,0,0,0);

        [NSTimer scheduledTimerWithTimeInterval: 0.01 target: self
                                       selector: @selector(callTheTarget:) userInfo: nil repeats: NO];
    }
Ravi_Parmar
  • 12,249
  • 2
  • 22
  • 36