【问题标题】:how to ignore UICollectionViewCell size on storyboard?如何忽略情节提要上的 UICollectionViewCell 大小?
【发布时间】:2018-09-04 18:28:45
【问题描述】:

故事板上有单元格大小:

但是我想根据设备的屏幕大小在代码中设置单元格大小,我调用下一个委托方法:

extension GraphicsViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 0.675 * self.view.frame.size.width, height: 0.625 * self.view.frame.size.height)
    }

}   

我称这个单元格的方法为:

func draw(coordinates: [CGPoint]?) {
        let width = self.temperatureGraphicView.frame.size.width
        let height = self.temperatureGraphicView.frame.size.height
        temperatureBackGroundView.layer.borderWidth = 2
        temperatureBackGroundView.layer.borderColor = UIColor.black.cgColor
        temperatureBackGroundView.layer.cornerRadius = 10
        humidityBackGroundView.layer.borderWidth = 2
        humidityBackGroundView.layer.borderColor = UIColor.black.cgColor
        humidityBackGroundView.layer.cornerRadius = 10
        let verticalLines = setFramesArrays().0
        let horizontalLines = setFramesArrays().1
        for i in stride(from: 0, to: verticalLines.count - 1, by: 2) {
            drawFrame(fromPoint: verticalLines[i], toPoint: verticalLines[i + 1], graphicNumber: 0)
            drawFrame(fromPoint: verticalLines[i], toPoint: verticalLines[i + 1], graphicNumber: 1)
        }
        for i in stride(from: 0, to: horizontalLines.count - 1, by: 2) {
            drawFrame(fromPoint: horizontalLines[i], toPoint: horizontalLines[i + 1], graphicNumber: 0)
            drawFrame(fromPoint: horizontalLines[i], toPoint: horizontalLines[i + 1], graphicNumber: 1)
        }
    }   

我的视图框架基于故事板上单元格的框架,而我的网格绘制不正确:

当我重新加载集合中的数据时,我有这个:

我的问题是如何忽略情节提要上的单元格框架并以编程方式设置它?我可以用输入视图框架做什么?

【问题讨论】:

    标签: ios swift uicollectionview uistoryboard


    【解决方案1】:

    我假设您在collectionView(collectionView:indexPath:) 数据源方法中调用draw 函数。 您必须调用 cell.layoutIfNeeded() before 以使单元格内的所有子视图使用新大小重新布局。

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2015-01-20
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2016-01-07
      • 2012-09-27
      相关资源
      最近更新 更多