iOS: UICollectionViewCell auto adjust size accordi

2019-05-01 06:30发布

I am trying to create 10 cells in the collection view(same size as the screen). When I run my app in iphone5s simulator, the view contains exactly 5 cells. But when I switch to iphone6p simulator, the view contains more than 5 cells. How should I adjust the cell size so that the number of cells in screen are consistent across different screen sizes?

2条回答
We Are One
2楼-- · 2019-05-01 06:36
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


return CGSizeMake([(NSString*)[[tagArray objectAtIndex:indexPath.row] objectForKey:@"tag"] sizeWithAttributes:NULL].width+10, 40);

}
查看更多
不美不萌又怎样
3楼-- · 2019-05-01 06:41
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   int numberOfCellInRow = 3;
   CGFloat cellWidth =  [[UIScreen mainScreen] bounds].size.width/numberOfCellInRow;
   return CGSizeMake(cellWidth, cellWidth);
}
查看更多
登录 后发表回答