1

The horizontal scrolling in UICollectionView only returns one row and I need to make a horizontal scrolling collection view with 2 rows just like the image below

[1]: https://i.sstatic.net/Vs1kt.png :[1]

1 Answer 1

5

You need to set fix height of the CollectionView and then use sizeForItemAt() under UICollectionViewDelegateFlowLayout which returns CGSize. You have to manage the cell height something like this.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            return CGSize(width: 50.0, height: collectionViewHeight / 2) //<-- Manage height and width accordingly.
        }
Sign up to request clarification or add additional context in comments.

4 Comments

and how the second row will appear ? coz sizeforitemAt will only manage size .
Set scroll direction to horizontal and fix height of CollectionView. Now use the above method. Don't forget to Add UICollectionViewDelegateFlowLayout protocol to your ViewController.
should i increase the height for collectionview so that it can accommodate two cells ?
Yes, you can manage accordingly also refer : stackoverflow.com/questions/17229350/… for cell spacing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.