问题
I'm trying to figure out how to setup the frame of the UICollectionView cell to the frame of the view (similar of how photo app) but I need to do it programmatically. Any of you knows how can I do this?
I'll really appreciate your help.
回答1:
You can use the sizeForItemAt indexPath
method on UICollectionViewDelegateFlowLayout
. This will determine the size of each cell, and can be configured to be relative to the size of other elements (like your view
, for example).
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width / 2, height: 50)
}
Just make sure the deleagte
of your collection view is set, and the delegate conforms to the UICollectionViewDelegateFlowLayout
protocol.
来源:https://stackoverflow.com/questions/43812424/iossetting-uicollectionview-cell-to-view-size