Setting size of UICollectionViewCell in a way that there is no interim spacing in between

前端 未结 4 1808
时光取名叫无心
时光取名叫无心 2021-01-28 23:36

I\'m using an UICollectionView on which I want to place seven cells side by side. The whole screen should be used for this. Currently, I\'m using the width of the c

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 00:13

    Try this out, declare these variables

      NSInteger insets_Top=5;
      NSInteger insets_Bottom=5;
      NSInteger insets_Left=5;
      NSInteger insets_Right=5; 
    

    and add these methods

     - (CGSize)collectionView:(UICollectionView *)collectionView layout:   (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:   (NSIndexPath *)indexPath
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;
        CGSize img_size=CGSizeMake(screenWidth/num_items-   (insets_Right+insets_Left), screenWidth/num_items);
        return img_size;
    }
    
      - (UIEdgeInsets)collectionView:
     (UICollectionView *)collectionView layout:  (UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:  (NSInteger)section
       {
          return    UIEdgeInsetsMake(insets_Top,insets_Left,insets_Bottom,insets_Right);
       }
    

    Here num_items=7 as in your case you want number of cells in a row to be 7

提交回复
热议问题