如何忽略故事板上的 UICollectionViewCell 大小?
how to ignore UICollectionViewCell size on storyboard?
我在故事板上有单元格大小:
但是我想根据设备的屏幕大小在代码中设置单元格大小,然后调用下一个委托方法:
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)
}
}
我的视图框架基于故事板上单元格的框架,而我的网格绘图不正确:
当我重新加载集合中的数据时,我有这个:
我的问题是如何忽略故事板上的单元格框架并以编程方式设置它?我可以用输入视图框架做什么?
我假设您在 collectionView(collectionView:indexPath:)
数据源方法中调用 draw
函数。
您必须在 行之前调用 cell.layoutIfNeeded()
以使用新大小重新布局单元格中的所有子视图。
我在故事板上有单元格大小:
但是我想根据设备的屏幕大小在代码中设置单元格大小,然后调用下一个委托方法:
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)
}
}
我的视图框架基于故事板上单元格的框架,而我的网格绘图不正确:
当我重新加载集合中的数据时,我有这个:
我的问题是如何忽略故事板上的单元格框架并以编程方式设置它?我可以用输入视图框架做什么?
我假设您在 collectionView(collectionView:indexPath:)
数据源方法中调用 draw
函数。
您必须在 行之前调用 cell.layoutIfNeeded()
以使用新大小重新布局单元格中的所有子视图。