0

我编写了返回单元格大小的函数UICollectionView

   fileprivate func getCellSize(with row: Int) -> CGSize {

        let percentage: CGFloat = row == 0 ? 0.8 : 0.2
        let width = self.collectionView?.frame.width
        let height = self.collectionView?.frame.height

        let expectedWidth = width! * percentage
        let expectedHeight = height! * 0.5

        return CGSize(width: expectedWidth, height: expectedHeight)
    }

这个功能工作正常,但它有一个小问题,与我的圆角接缝有关。因为我收到的布局没有像预期的那样被单元格完全覆盖。

iPhone 6 模拟器的函数结果如下:

0:行 = (533.60000000000002,187.5)

1:行 = (133.40000000000001,187.5)

实际结果:

在此处输入图像描述

预期结果:

在此处输入图像描述

4

1 回答 1

2

怎么样

if let frameWidth = self.collectionView?.frame.width {
     let row0Width = Int(Double(frameWidth) * 0.8)
     let otherWidth = frameWidth - row0Width
     let expectedWidth = row == 0 ? row0Width : otherWidth
     // ...
}

避免舍入问题?

于 2017-05-11T08:45:16.590 回答