方向改变时扭曲的角半径
Distorted corner radius on orientation change
我有以下代码,方向在加载时效果很好,但是当方向改变时它会变形,也就是说,如果它以纵向加载并更改为横向,角半径会变形,就像它发生变化一样从圆到方。
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
}
如何避免这种情况发生?有没有办法在 viewDidLayoutSubviews()
中调用 cellForItemAt indexpath
,然后在那里配置 corner radius
?
你能试试吗
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
}
我有以下代码,方向在加载时效果很好,但是当方向改变时它会变形,也就是说,如果它以纵向加载并更改为横向,角半径会变形,就像它发生变化一样从圆到方。
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
}
如何避免这种情况发生?有没有办法在 viewDidLayoutSubviews()
中调用 cellForItemAt indexpath
,然后在那里配置 corner radius
?
你能试试吗
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
}