how to make dynamic width of the UICollectionViewCell

前端 未结 5 1334
粉色の甜心
粉色の甜心 2021-01-03 05:59

I want to set the width of UICollectionViewCell dynamically. I found some code in swift and I want the code in Objective-C.

let flowLayout = col         


        
5条回答
  •  礼貌的吻别
    2021-01-03 06:37

    This is the simplest solution.

    Your UIViewController should implement UICollectionViewDelegateFlowLayout. Then the function below needs to be implemented on your UIviewController.

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        let width = self.collectionView.frame.size.width / 2;
    
    
        return CGSize(width: width, height: 230)
    
    }
    

    Furthermore, you can adjust each cell's spacing value on your storyboard. Click collectionView then see the size inspector.

提交回复
热议问题