简体   繁体   中英

Distorted corner radius on orientation change

I have the following code, and the orientation works well when it loads but when the orientation is changed it gets distorted, that is if it loads in portrait and change to landscape, the corner radius gets distorted, like it changes from circle to square.

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
}

How to avoid this from happening? Is there a way to call cellForItemAt indexpath in viewDidLayoutSubviews() and then configuring corner radius there ?

Can you try

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{

    self.collectionView.reloadData()

    self.collectionView.layoutIfNeeded()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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