Ios 在iPad上按下HOME(主页)按钮后CollectionView崩溃
我有一个CollectionView,并动态设置其单元格的大小。问题是:当我点击“主页”按钮并再次进入游戏时,CollectionView崩溃了。所有的细胞看起来都很糟糕,它们的大小和位置都不正确。而且这种情况只发生在iPad上,在iPhone上也能完美运行。 以下是我创建表的代码:Ios 在iPad上按下HOME(主页)按钮后CollectionView崩溃,ios,swift3,uicollectionview,Ios,Swift3,Uicollectionview,我有一个CollectionView,并动态设置其单元格的大小。问题是:当我点击“主页”按钮并再次进入游戏时,CollectionView崩溃了。所有的细胞看起来都很糟糕,它们的大小和位置都不正确。而且这种情况只发生在iPad上,在iPhone上也能完美运行。 以下是我创建表的代码: if collectionView == tableCollectionView{ let cell:TableViewCell = collectionView.dequeueReusableC
if collectionView == tableCollectionView{
let cell:TableViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "letter_cell", for: indexPath) as! TableViewCell
let size = Int(collectionView.frame.size.width) / (lettersCountInRow + 1);
let j = indexPath.row
let i = indexPath.section
let letter = guessLogic!.letters[i * lettersCountInRow + j]
let imageName = String(describing: letter)
let vertical_shift = (Int(collectionView.frame.size.height) - size * 2) / 2
let pad = Double(size) * 0.1;
let start_x = Int(collectionView.frame.size.width) - size*7 - Int(pad) * lettersCountInRow;
cell.letterImage.image = UIImage(named: imageName)
cell.frame = CGRect(x:start_x + j * Int(pad) + j * size, y: i * size + vertical_shift + Int(pad), width: size, height: size)
return cell
}
在iPad Air-2上测试
这就是表格的外观:
这就是我按下HOME键返回游戏后的样子:
您是否在viewdidload()中设置了单元格的大小?
在这种情况下,尝试在viewdidappear()中执行此操作,然后查看。好吧,我找到了解决方案。这是因为方向。我禁用了横向定位,一切都开始正常工作。否,我使用此方法调整单元格大小**func collectionView(\uCollectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell**您可以提供如何设置单元格大小的代码吗?请添加一些屏幕截图。请,查看问题中的新信息