简体   繁体   中英

UICollectionView: dynamic cell width for horizontal scrolling

I have a collection view with several labels inside located in one line. This collection view has to have horizontal scrolling and cell width calculated dynamically based on the label's sizes. I found the solution on how to make cell's width dynamic here: Dynamic cell width of UICollectionView depending on label width

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return (labels[indexPath.row] as NSString).size(attributes: nil)
}

But this method is only called when the class conforms to UICollectionViewDelegateFlowLayout which has vertical scrolling direction that doesn't suit me.

How can I make dynamic cell width for default layout with horizontal scrolling?

Have you tried setting the scroll direction of your UICollectionViewFlowLayout to horizontal?

yourFlowLayout.scrollDirection = .horizontal

Apple Documentation for this

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