iOS – UICollectionView间距精度

我有一个简单的4 x 2集合视图网格,如下所示:

class ExampleViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = 0 self.collectionView = UICollectionView(frame: CGRect.init(x: 0.0, y: 100.0, width: 414.0, height: 226.0), collectionViewLayout: layout) self.collectionView.delegate = self self.collectionView.dataSource = self self.collectionView.showsVerticalScrollIndicator = false self.collectionView.showsHorizontalScrollIndicator = false self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(ExampleViewController.self)) self.view.addSubview(self.collectionView) } public func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 8 } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(ExampleViewController.self), for: indexPath) cell.backgroundColor = .cyan return cell } public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = CGSize(width: self.collectionView.frame.width / 4.0, height: self.collectionView.frame.height / 2.0) return size } } 

我们在这里忽略所有硬编码的数字。 一切都很简单,我应该得到一个4 x 2网格,单元格之间没有任何间距。 然而,我得到的非常接近,但并不完全相同:某些细胞之间有一些空间。

这是我从recursiveDescription得到的:

 <UICollectionView: 0x7f8fdf886400; frame = (0 100; 414 226); clipsToBounds = YES; gestureRecognizers = ; layer = ; contentOffset: {0, 0}; contentSize: {414, 226}> collection view layout:  | <UICollectionViewCell: 0x7f8fddc103c0; frame = (0 0; 103.5 113); layer = > | | <UIView: 0x7f8fddc10ac0; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc10ff0; frame = (103.667 0; 103.5 113); layer = > | | <UIView: 0x7f8fddc11410; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc11700; frame = (207 0; 103.5 113); layer = > | | <UIView: 0x7f8fddc11910; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc11c00; frame = (310.667 0; 103.5 113); layer = > | | <UIView: 0x7f8fddc11e10; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc12100; frame = (0 113; 103.5 113); layer = > | | <UIView: 0x7f8fddc12310; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc12600; frame = (103.667 113; 103.5 113); layer = > | | <UIView: 0x7f8fddc12810; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc12b00; frame = (207 113; 103.5 113); layer = > | | <UIView: 0x7f8fddc12d10; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > | <UICollectionViewCell: 0x7f8fddc13240; frame = (310.667 113; 103.5 113); layer = > | | <UIView: 0x7f8fddc13450; frame = (0 0; 103.5 113); gestureRecognizers = ; layer = > 

请注意,有些frame.origin.x的单元格为103.667和310.667,但实际上应该是103.5和310.5。 那非常接近,但并不完全。

我错过了一些配置,还是在UICollectionView的实现中出现了某种浮点计算错误?