【问题标题】:iOS: UICollectionViewCell auto adjust size according to screen sizeiOS:UICollectionViewCell 根据屏幕大小自动调整大小
【发布时间】:2015-07-16 12:16:29
【问题描述】:

我正在尝试在集合视图中创建 10 个单元格(与屏幕大小相同)。当我在 iphone5s 模拟器中运行我的应用程序时,视图正好包含 5 个单元格。但是当我切换到 iphone6p 模拟器时,视图包含超过 5 个单元格。我应该如何调整单元格大小,以使屏幕中的单元格数量在不同的屏幕尺寸下保持一致?

【问题讨论】:

标签: ios objective-c swift uicollectionview uicollectionviewcell


【解决方案1】:
-(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);
}

【讨论】:

  • 设置您的collectionView宽度以编程方式随屏幕宽度变化。
  • 如果单元格之间有空间,请在计算单元格宽度时考虑它。
【解决方案2】:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


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

}

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多