【问题标题】:Calculations of the 80% of screen and 20% in UICollectionView计算 80% 的屏幕和 20% 的 UICollectionView
【发布时间】:2017-05-11 08:12:20
【问题描述】:

我编写了返回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: ROW = (533.60000000000002,187.5)

1:行 = (133.40000000000001,187.5)

实际结果:

预期结果:

【问题讨论】:

  • 那么您期望它是什么以及这里实际发生了什么?
  • 这显然不是真正的代码(百分比与 peresenteg)。您是否尝试过调试器来找出与您的预期不同的值?
  • @Sandeep 我添加了屏幕。
  • 您似乎有一些部分插入和单元格之间的一些间距,删除它应该没问题,看起来像那样。

标签: ios swift uicollectionview size cgsize


【解决方案1】:

怎么样

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

为了避免舍入问题?

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多