iOS:Setting UICollectionView cell to view size

情到浓时终转凉″ 提交于 2019-12-12 05:20:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!