在iPhone13 pro真机和测试机上CollectionView Cell 换行情况

场景描述(踩空过程)

本来是CollectionView两列展示的布局,在模拟器(xcode模拟器是iPhone15)上开发过程显示很正常、很符合预期。之后换iPhone13真机测试两列变一列,回过头用iPhone13的模拟器也是一样效果。打印计算出来的数值对比都是正确的,真让人头秃啊!

后来在StackOverflow上搜到有人遇到,他解决方法是计算出来的item宽度用floor下取整。但到我这里没有直接取到效果,再把间距调小才重新显示两列。

ε=(´ο`*)))唉,汗颜~ 过程让人头秃~

其实总体思路是给横向水平上计算的值比设计长度短点,给间距minimumInteritemSpacing多留空间,这样可以从间距调小和item调小两个方面入手。

解决方案

以下是遇到情况的代码(其他代码部分就是平常的CollectionView写法):

ini 复制代码
// settingCollectionviewItem()方法在viewDidload中调用的
func settingCollectionviewItem() {

    let columnCount = 2

    let horizontalMargin: CGFloat = 14
    let verticolMargin: CGFloat = 14

    let flowLayout = UICollectionViewFlowLayout()
        
    flowLayout.minimumLineSpacing = verticolMargin
    flowLayout.minimumInteritemSpacing = horizontalMargin
        
    flowLayout.sectionInset = UIEdgeInsets(top: verticolMargin, left: horizontalMargin, bottom: verticolMargin, right: horizontalMargin)

    let colloctionWidth = myrCollectionView.frame.width

    let itemWidth = (colloctionWidth - (CGFloat(columnCount - 1) * horizontalMargin + flowLayout.sectionInset.left + flowLayout.sectionInset.right)) / CGFloat(columnCount)

    // Item宽高比152:220
    let itemHeight = itemWidth * (220 / 152)

    flowLayout.itemSize = CGSize(width: itemWidth, height: itemHeight)

    myrCollectionView.collectionViewLayout = flowLayout
}
    

以下是采取的措施 (有更好的处理方法望评论区告知,还有知道为什么这个机型显示有问题也麻烦告知,不胜感激!):

ini 复制代码
func settingCollectionviewItem() {

    let columnCount = 2

    let horizontalMargin: CGFloat = 14
    let verticolMargin: CGFloat = 14

    let flowLayout = UICollectionViewFlowLayout()

    flowLayout.minimumLineSpacing = verticolMargin
    flowLayout.minimumInteritemSpacing = horizontalMargin - 2

    flowLayout.sectionInset = UIEdgeInsets(top: verticolMargin, left: horizontalMargin, bottom: verticolMargin, right: horizontalMargin)

    let colloctionWidth = myCollectionView.frame.width

    let itemWidth = floor((colloctionWidth - (CGFloat(columnCount - 1) * horizontalMargin + flowLayout.sectionInset.left + flowLayout.sectionInset.right)) / CGFloat(columnCount))

    // Item宽高比152:220
    let itemHeight = itemWidth * (220 / 152)

    flowLayout.itemSize = CGSize(width: itemWidth, height: itemHeight)

    myCollectionView.collectionViewLayout = flowLayout
}

参考文章

CollectionViewCells not displaying in middle on iPhone 13 Pro only

相关推荐
Swift社区6 小时前
iOS 基于 Foundation Model 构建媒体流
ios·iphone·swift·媒体
库奇噜啦呼8 小时前
【iOS】音频与视频播放
ios·音视频·cocoa
大熊猫侯佩16 小时前
黑衣人档案:用 Apple Foundation Models + SwiftUI 打造 AI 聊天机器人全攻略
ios·swiftui·ai编程
大熊猫侯佩16 小时前
侠客行・iOS 26 Liquid Glass TabBar 破阵记
ios·swiftui·swift
2501_9160074718 小时前
手机使用过的痕迹能查到吗?完整查询指南与步骤
android·ios·智能手机·小程序·uni-app·iphone·webview
從南走到北19 小时前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
alengan21 小时前
苹果企业签名流程
ios·iphone
Digitally1 天前
如何在Mac上同步iPhone短信
macos·ios·iphone
2501_915106321 天前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
2501_916008891 天前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview