You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS14.4及更早版本UICollectionView Compositional Layout无法滚动到最后一项问题

问题诱因分析

这个问题是iOS 14.4及更早版本中UICollectionViewCompositionalLayout的已知系统Bug,核心触发原因如下:

  • 当给配置了正交滚动行为(orthogonalScrollingBehavior)的section设置contentInsets时,iOS 14.4及更早版本的系统在计算该正交滚动区域的contentSize时,遗漏了trailing方向的contentInsets数值,导致最终生成的可滚动区域宽度比实际需要的宽度小,刚好缺少你设置的16pt trailing间距的空间,所以最后一个item/group滚动到边缘就会被截断,无法完整展示。
  • 苹果在iOS 14.5的系统更新中修复了这个布局计算的逻辑缺陷,会自动将leading和trailing的contentInsets都纳入正交滚动contentSize的计算范围,因此更高版本运行完全正常。

兼容方案参考

如果需要适配iOS 14.4及更早版本,可以采用以下方案规避问题:

if #available(iOS 14.5, *) {
    section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
} else {
    // 低版本先移除trailing inset,避免计算错误
    section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 0)
    // 新增宽度匹配trailing间距的尾部补充视图,补全尾部空白
    section.boundarySupplementaryItems = [
        NSCollectionLayoutBoundarySupplementaryItem(
            layoutSize: .init(widthDimension: .absolute(16), heightDimension: .fractionalHeight(1.0)),
            elementKind: "TrailingPaddingSupplementaryView",
            alignment: .trailing
        )
    ]
}

你只需要注册对应的空白补充视图,即可实现和高版本一致的展示效果。

内容的提问来源于stack exchange,提问作者figiel99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:54:03