Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Horizontal scroll #2

Open
rnkyr opened this issue Jul 20, 2021 · 6 comments
Open

[Question] Horizontal scroll #2

rnkyr opened this issue Jul 20, 2021 · 6 comments

Comments

@rnkyr
Copy link

rnkyr commented Jul 20, 2021

Hey, @nemecek-filip
Thanks for the article.

I recently faced a bit similar task but ended up resolving it without compositional layout.
So the question is, is there any possibility to have a horizontal scroll in the photos section (slice D from the article), so you scroll among tabs from the header?

@nemecek-filip
Copy link
Owner

Hi!

Yes, you would probably need to create nested groups to make the 3x3 grid for example and then you could set the orthogonalScrollingBehavior property to continuous for example.

The article in question for others: https://nemecek.be/blog/72/building-instagram-profile-screen-with-compositional-layout

Btw, I am curious how did you find the article? :-)

@rnkyr
Copy link
Author

rnkyr commented Jul 21, 2021

I'll take a look at it, thank you!

Found your article in the mail subscription from here https://mbltdev.ru/en

@nemecek-filip
Copy link
Owner

Interesting! Thanks for the info.

Thinking about the photos.. I realized that you possibly don't even need nested groups. Below is slightly modified and untested code for the photos section if you want to try it out:

func createPhotosSection() -> NSCollectionLayoutSection {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                          heightDimension: .fractionalHeight(1.0))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)

    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1/3),
                                           heightDimension: .fractionalWidth(1))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3)

    let section = NSCollectionLayoutSection(group: group)

    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(50))

    let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)

    section.boundarySupplementaryItems = [header]
    section.orthogonalScrollingBehavior = .continuous

    return section
}

Note that I made the group vertical, changed its fractional size and set the orthogonalScrollingBehavior

@SMH-7
Copy link

SMH-7 commented Sep 14, 2022

It aligns in the vertical direction but still scrolls in the horizontal direction...
Any clue on how to implement that?

@nemecek-filip
Copy link
Owner

Hi @SMH-7 I am not sure I understand the question.

I think you could use contentInsets property on the horizontal section to give it perhaps a bit of leading padding

@SMH-7
Copy link

SMH-7 commented Sep 15, 2022

My default flowLayout was
HorziLayout.scrollDirection = .vertical
but I decided to divide the collection view into multiple sections but still go with a vertical scroll for both of the sections.
The following code aligns them in a vertical stack but the thing is, now they scroll horizontally.
let group = NSCollectionLayoutGroup.vertical(layoutSize: .init(widthDimension: .fractionalWidth(0.33), heightDimension: .fractionalHeight(0.33)), subitems: [item])
How do I align vertically and scroll vertically as well?
Have another question...
When you setup size for NSCollectionLayoutItem or NSCollectionLayoutGroup via fractionalHeight\fractionalWeight it adjusts according to the collection view layout. Initially, if we gave the appropriate size for individual cells for a specific number of cells in a col in sizeForItemAt that is basically getting overridden here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants