UICollectionView display 3 items per row

后端 未结 7 582
离开以前
离开以前 2020-12-16 03:30

I have a UICollectionView created from storyboard,

I want to have 3 items per row in the view. I managed to do that using the following:

    - (CGSiz         


        
相关标签:
7条回答
  • 2020-12-16 04:24
    (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
      CGRect screenRect = [[UIScreen mainScreen] bounds];
      CGFloat screenWidth = screenRect.size.width;
      float cellWidth = screenWidth / 3.2; //Replace the divisor with the column count requirement. Make sure to have it in float.
      CGFloat screenHeight = screenRect.size.height;
      float cellHeight = screenHeight/3.0;
    
    
      CGSize size = CGSizeMake(cellWidth, cellHeight);
    
    
      return size;
    }
    

    Apply the below code too-(viewWillTransitionToSize)

    (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
      [self.collectionView/*(that's my collection view name)*/ reloadData];
    }
    

    This Code will help to get the same no. of cells in portrait as well as in landscape mode also. I have applied above code to get 3 cells in portrait as well as 3 cells in landscape mode.

    0 讨论(0)
提交回复
热议问题