iOS:UICollectionViewCell 根据屏幕大小自动调整大小

标签 ios objective-c swift uicollectionview uicollectionviewcell

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

最佳答案

-(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);
}

关于iOS:UICollectionViewCell 根据屏幕大小自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075408/

相关文章:

json - Swift 和 Alamofire API 调用 : trouble reading parameters

ios - 在 UIWebView 中加载 NSURLRequest - 堆不断增长

android - 移动开发(Android & iOS)初学者需要指导

ios - 使用 Swift p2/OAuth2 并行刷新 OAuth2 访问 token 请求

ios - Objective C Google Maps API 公钥函数

swift - 如何在不转换为 Swift 中的 String 的情况下拆分或迭代 Int

ios - 如何在iOS中求解方程式

ios - 单例 - 错误 : [CFString isNSString__] message sent to deallocated instance

ios - 从 ACAccountstore 获取 Twitter 用户名 -

ios - 从 xib 文件加载 ViewController