10

I'm trying to print the image on fullscreen I used UICollectionViewDelegateFlowLayout here my code is

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWidth = collectionView.bounds.width
        let itemHeight = collectionView.bounds.height
        return CGSize(width: itemWidth, height: itemHeight)
    }

but it doesn't show the content on full screen it makes space from top and bottom

AfterCoder
  • 731
  • 2
  • 6
  • 22

1 Answers1

17

If you want to increase cell size to fit to your screen you can use this method.

Objective-C

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}

Swift

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //For entire screen size
        let screenSize = UIScreen.main.bounds.size
        return screenSize
        //If you want to fit the size with the size of ViewController use bellow
        let viewControllerSize = self.view.frame.size
        return viewControllerSize

        // Even you can set the cell to uicollectionview size
        let cvRect = collectionView.frame
        return CGSize(width: cvRect.width, height: cvRect.height)


    }
Alirza Eram
  • 134
  • 11
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • 1
    I'm able to do that now. Is it rational or is it right way to present a view in collectionview cell? Is there a better way to do this. – AfterCoder Oct 31 '16 at 23:09
  • 5
    Be sure to also set collection view's Estimate Size to None in the storyboard, otherwise the size won't change even though the above sizeForItemAt method is being called. – Smartcat Jan 06 '21 at 18:02