swift - UICollectionView 单元格间距相等

标签 swift uicollectionview tvos

我在 Collection View 中显示多张图片。它们都是相同的方向(横向),除了一个是纵向(见下图):

enter image description here

我正在尝试使肖像图像更居中,以便所有内容均匀分布。关于如何做到这一点的任何想法?

PhotoGalleryViewController.swift:

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoGalleryCell

    let image = UIImage(named: images[indexPath.row])
    cell.imageView.image = image
    cell.captionLabel.text = captions[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var size = CGSize()

    if (indexPath.row == 3)
    {
        size = CGSize(width: 450, height: 600)
    }
    else {
        size = CGSize(width: 940, height: 600)

    }
    return size
}

最佳答案

我的建议是不要根据肖像图像将在项目 3 中的事实更改单元格大小。

而是更新您的单元格的布局,以便无论将其分配给其 ImageView 的图像如何,它都会将图像本身居中并将标签集中在图像下方。

关于swift - UICollectionView 单元格间距相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42768321/

相关文章:

iOS:如何制作多列表

ios - 如何正确地使 UICollectionView 中补充 View 的布局无效

tvOS 框架包含不完整的位码

swift - UIImage View 在向后导航时丢失图像

swift - 如何从另一个 ViewController 更新 LineChart

ios - 如何正确设置分页 UICollectionView 大小变化的动画

ios - 无缝循环 AVPlayer

ios - UICollectionView 如何找到单元格结束的行

ios - Swift 3.0 - NSNumber 和 Int 不能应用于操作数

ios - 如何将选定单元格中的数据传递到另一个 View Controller ?