ios - UICollectionView 自动布局和一行中的项目数

标签 ios uicollectionview uicollectionviewcell

我有一个关于 UICollectionView 的查询。

我有一个用于 UICollectionViewCell 的自定义单元子类。每个单元格包含 1 个 ImageView 、一个进度条和两个标签。我能够使用自定义单元格在 UICollectionView 中显示所有数据。

现在我的问题是:- 如果我在 4 英寸设备上运行,如何连续显示一个项目,如果在 iPhone 6 和 6s 上运行,我如何连续显示两个项目?此外,所有项目都是 UICollectionViewCell 应该安排好。

我知道我可以使用尺寸等级,但如果有人有比这种方法更好的主意,那会很好。

最佳答案

您可以根据您的要求使用它,为设备设置条件

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    float cellWidth = screenWidth / 2.0;  //put number of columns you require
    CGSize size = CGSizeMake(cellWidth, cellWidth);

    return size;
}

关于ios - UICollectionView 自动布局和一行中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38141225/

相关文章:

ios - UIImagePickerController() 在 UIImage View 中显示图像很好,但在集合和表的 UIImage View 中旋转?

ios - 以编程方式将 UICollectionViewCell 设置为 Selected 但 indexPathsForSelectedItems.count 为 0

ios - 'UICollectionViewCell' 没有名为 'image' [Swift] 的成员

ios - 在 performBatchUpdates 中重新加载 collectionview 时未调用 UICollectionview cellForItemAtIndexPath

ios - UITableViewController 返回到单元格中的下一个文本字段

ios - tableView cellForRowAtIndexPath : runs before ViewDidLoad?

ios - 具有自定义布局的 UICollectionView 布局

ios - 警告 : CLSLog has been used before (or concurrently with) Crashlytics initialization and cannot be recorded

swift - 如何访问我的存储并将其放置在我的收藏 View 中

ios - Swift:让 UICollectionView 动态改变它的大小