I have a simple 4 x 2 data collection grid:
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 } }
Let all hard-coded numbers be ignored here. Everything is simple, and I have to get a 4 x 2 grid without any gap between cells. However, I have a very close one, but not quite that: there is some space between some cells.
Here is what I got from recursiveDescription:
<UICollectionView: 0x7f8fdf886400; frame = (0 100; 414 226); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x618000241440>; layer = <CALayer: 0x618000422e80>; contentOffset: {0, 0}; contentSize: {414, 226}> collection view layout: <UICollectionViewFlowLayout: 0x7f8fdde2a510> | <UICollectionViewCell: 0x7f8fddc103c0; frame = (0 0; 103.5 113); layer = <CALayer: 0x608000222460>> | | <UIView: 0x7f8fddc10ac0; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x608000240cc0>; layer = <CALayer: 0x608000222220>> | <UICollectionViewCell: 0x7f8fddc10ff0; frame = (103.667 0; 103.5 113); layer = <CALayer: 0x6080002223e0>> | | <UIView: 0x7f8fddc11410; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x608000240780>; layer = <CALayer: 0x608000222180>> | <UICollectionViewCell: 0x7f8fddc11700; frame = (207 0; 103.5 113); layer = <CALayer: 0x608000222580>> | | <UIView: 0x7f8fddc11910; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x608000240720>; layer = <CALayer: 0x608000221fe0>> | <UICollectionViewCell: 0x7f8fddc11c00; frame = (310.667 0; 103.5 113); layer = <CALayer: 0x608000222340>> | | <UIView: 0x7f8fddc11e10; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x608000240ed0>; layer = <CALayer: 0x608000222720>> | <UICollectionViewCell: 0x7f8fddc12100; frame = (0 113; 103.5 113); layer = <CALayer: 0x60800003fd20>> | | <UIView: 0x7f8fddc12310; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x6080002414d0>; layer = <CALayer: 0x6080002227a0>> | <UICollectionViewCell: 0x7f8fddc12600; frame = (103.667 113; 103.5 113); layer = <CALayer: 0x608000222d40>> | | <UIView: 0x7f8fddc12810; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x6080002416e0>; layer = <CALayer: 0x608000222960>> | <UICollectionViewCell: 0x7f8fddc12b00; frame = (207 113; 103.5 113); layer = <CALayer: 0x6080002222a0>> | | <UIView: 0x7f8fddc12d10; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x6080002418f0>; layer = <CALayer: 0x6080002224e0>> | <UICollectionViewCell: 0x7f8fddc13240; frame = (310.667 113; 103.5 113); layer = <CALayer: 0x608000222900>> | | <UIView: 0x7f8fddc13450; frame = (0 0; 103.5 113); gestureRecognizers = <NSArray: 0x608000241b00>; layer = <CALayer: 0x6080002228c0>>
Note that there are cells with frame.origin.x that make up 103.667 and 310.667, but in fact it should be 103.5 and 310.5. It is very close, but not quite.
Am I missing some configurations or is it some kind of floating point calculation error in the UICollectionView implementation?
source share