繁体   English   中英

如何忽略情节提要上的UICollectionViewCell大小?

[英]how to ignore UICollectionViewCell size on storyboard?

我在情节提要上有单元格大小:
屏幕截图1
但是我想根据设备的屏幕大小在代码中设置单元格大小,然后调用下一个委托方法:

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)
        }
    }   

我的视图框架是基于情节提要中单元格的框架,并且我的网格图形不正确:
屏幕截图2

当我重新加载集合中的数据时,我有以下内容:
屏幕截图3
我的问题是如何忽略情节提要上的单元格框架并以编程方式设置它? 我该如何使用输入视图框架?

我假设您正在collectionView(collectionView:indexPath:)数据源方法中调用draw函数。 您必须该行之前调用cell.layoutIfNeeded() 以使用新的大小在单元cell.layoutIfNeeded()内进行所有子视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM