在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

相关推荐
叽哥6 小时前
Flutter Riverpod上手指南
android·flutter·ios
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间2 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h3 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview