Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在单元格之间以及边距和单元格之间创建相等的空间UICollectionView_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 在单元格之间以及边距和单元格之间创建相等的空间UICollectionView

Ios 在单元格之间以及边距和单元格之间创建相等的空间UICollectionView,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个UICollectionView。在某些设备上,电池紧贴设备的边缘,而在中心有一个很大的间隙。我尝试过改变插图和最小间距,这两个都不起作用。我在故事板中创建了它,因此没有与格式相关的代码。如何使细胞之间的空间等于外细胞和边缘之间的空间,所以中间没有一个巨大的间隙?谢谢 图像的边缘是设备的精确边缘假设您使用的是UICollectionViewFlowLayout,并且您的单元格大小固定: 自定义单元格代码:(我添加了一些阴影和圆角,忽略它) 视图控制器代码: 结果如下: import

我有一个UICollectionView。在某些设备上,电池紧贴设备的边缘,而在中心有一个很大的间隙。我尝试过改变插图和最小间距,这两个都不起作用。我在故事板中创建了它,因此没有与格式相关的代码。如何使细胞之间的空间等于外细胞和边缘之间的空间,所以中间没有一个巨大的间隙?谢谢


图像的边缘是设备的精确边缘

假设您使用的是
UICollectionViewFlowLayout
,并且您的单元格大小固定:

自定义单元格代码:(我添加了一些阴影和圆角,忽略它) 视图控制器代码: 结果如下:

import UIKit

class CollectionViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)


        layer.shadowColor = UIColor.lightGray.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2.0)
        layer.shadowRadius = 5.0
        layer.shadowOpacity = 1.0
        layer.masksToBounds = false
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath
        layer.backgroundColor = UIColor.clear.cgColor

        contentView.layer.masksToBounds = true
        layer.cornerRadius = 10
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        /// Class registration
        collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        /// Expected cell size
        let cellSize = CGSize(width: 120, height: 110)

        /// Number of columns you need
        let numColumns: CGFloat = 2
        /// Interitem space calculation
        let interitemSpace = ( UIScreen.main.bounds.width - numColumns * cellSize.width ) / (numColumns + 1)

        /// Setting content insets for side margins
        collectionView.contentInset = UIEdgeInsets(top: 10, left: interitemSpace, bottom: 10, right: interitemSpace)

        /// Telling the layout which cell size it has
        (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = cellSize
    }

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.backgroundColor = .white
        return cell
    }
}