我有以下代码,并且在加载时方向效果很好但是当方向改变时它会变形,即如果它以纵向加载并变为横向,则角半径会变形,就像它从圆形变化一样广场。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = categoryCV.dequeueReusableCell(withReuseIdentifier: "CategoryCVC", for: indexPath) as! CategoryCollectionViewCell
cell.catLabel.text = cities[indexPath.row].name
cell.catImg.image = UIImage(named:cities[indexPath.row].image)
cell.layer.cornerRadius = cell.layer.frame.size.height/2
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.borderWidth = 2
return cell
}
如何避免这种情况发生?有没有办法在cellForItemAt indexpath
中拨打viewDidLayoutSubviews()
,然后在那里配置corner radius
?
答案 0 :(得分:2)
你能试试吗
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
}