ios - 如何将 Collection View 单元格设置为所有屏幕的相等行单元格

标签 ios swift uicollectionview

我正在使用 swift 2.o Collection View 。在这一切工作正常。但是当我在不同的屏幕上运行时,每个单元格之间的宽度差距是不同的。我在 Collection View 的每一行中需要三个单元格, Collection View 中所有单元格的左、右、上、下有 8 px 的间隙。而且我还需要将单元格设置为角半径。我需要如下图: [![在此处输入图片描述][1]][1]

但是当我在 5s 屏幕上运行时,我会像这样,对于 6 屏幕,如第二张图片:

[![在此处输入图片描述][2]][2]

苹果 6:

[![在此处输入图片描述][3]][3]

请看左、右、下、上之间的间隙并不像我的第一张图片那样均匀。我需要像我的第一张图片一样。

我在 Storyboard中将单元格的最小行间距设置为 6。但是我无法像我的第一张图片那样。

请帮帮我。

我的 popvc.swift :

import UIKit

class popVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var tableData: [String] = ["RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI", "RESTAURANTS", "BANKS","COFFEE","PIZZA", "HOTELS", "TAXI","RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI"]
    var tableImages: [String] = ["img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png", "img7.png", "img8.png", "img9.png", "img10.png", "img11.png", "img11.png", "img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell
        cell.lblCell.text = tableData[indexPath.row]
        cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("Cell \(indexPath.row) selected")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

最佳答案

从尺寸检查器中的 Storyboard设置 CollectionViews Section Inset 为 Top = 8,Bottom = 8,Left = 8 和 Right = 8,它应该看起来像

enter image description here

并返回单元格的大小为

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

   //If you want your cell should be square in size the return the equal height and width, and make sure you deduct the Section Inset from it.
   return CGSizeMake((self.view.frame.size.width/3) - 16, (self.view.frame.size.width/3) - 45);
}

仅此而已,仅此而已。你应该看到我的结果:

enter image description here

enter image description here

enter image description here

关于ios - 如何将 Collection View 单元格设置为所有屏幕的相等行单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35864049/

相关文章:

iOS 5 推送通知权限

ios - 观看操作系统 2.0 Beta : Not Receiving notifications

iOS希望在按下按钮时延迟 View Controller 之间的连接

iphone - clang : error: no such file or directory:

ios - 如何执行从 UIView 到 ViewController 的导航

ios - 导航项中带有搜索栏的连续 View Controller 在动画推送和弹出时会导致 View 模糊

ios - 如何使用callkit 框架在iOS 原生ui 调用中显示来电图像?

ios - UICollectionView 单元格的 Material 波纹效果

swift - 为什么我的渐变层覆盖了我的 collectionView 单元格?

swift - 从 UICollectionView 中删除项目后更新/重新加载 Collection View