UICollectionView OrthogonalsCrollingBehavior gropavepingcented错误的行为

发布于 2025-01-19 21:54:21 字数 1142 浏览 4 评论 0原文

因此,我有一个带有以下布局的收集视图,

  private static func createChannelsCompositionalLayout() -> UICollectionViewLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.45))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets.top = 5
    item.contentInsets.bottom = 5
    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.87), heightDimension: .fractionalHeight(1))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 2)
    group.contentInsets.trailing = 10
    group.contentInsets.leading = 10
    group.contentInsets.bottom = 10
    let section = NSCollectionLayoutSection(group: group)
    section.orthogonalScrollingBehavior = .groupPagingCentered
    let layout = UICollectionViewCompositionalLayout(section: section)
    return layout
  }

基本上它可以水平滚动,每组垂直显示两个项目。 如果物品数量均匀,则可以正常工作;但是,如果项目数是奇数的,则当您滚动到最后一个组(其中包含1个项目而不是2个)时,它不会像2个项目那样完美地拍摄。它将更靠近屏幕的边缘,第二组将显示出不可取的第二组。 这是一个错误,还是我可以更改代码的东西?

我尝试使用插图和分数玩一些,但没有快乐。我还尝试在该部分中添加ContentInset,但这也无济于事。

So I have a collection view with the following layout

  private static func createChannelsCompositionalLayout() -> UICollectionViewLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.45))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets.top = 5
    item.contentInsets.bottom = 5
    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.87), heightDimension: .fractionalHeight(1))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 2)
    group.contentInsets.trailing = 10
    group.contentInsets.leading = 10
    group.contentInsets.bottom = 10
    let section = NSCollectionLayoutSection(group: group)
    section.orthogonalScrollingBehavior = .groupPagingCentered
    let layout = UICollectionViewCompositionalLayout(section: section)
    return layout
  }

Basically it scrolls horizontally with two items displayed vertically per group.
It works fine if the number of items is even; however if the number of items is odd, when you scroll to the last group (which would contain 1 item instead of 2), it wouldn't snap perfectly like it does with 2 items. It will be closer to the screen's edge and a bigger portion of the second to last group will be shown which is not desirable.
Is that a bug or is there something I could change in my code?

I tried playing a little bit with the insets and fractions but no joy. I also tried adding a contentInset to the section but that didn't help either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文