iOS - UICollectionView间距精度

时间:2017-06-06 02:24:50

标签: 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 = <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>>

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

我是否缺少某些配置,或者在UICollectionView的实现中是否存在某种浮点计算错误?

0 个答案:

没有答案