【问题标题】:Swift UICollectionView iPad Flow Layout Not WorkingSwift UICollectionView iPad流布局不起作用
【发布时间】:2018-11-03 07:13:33
【问题描述】:

目前在 iPad 上使用集合视图的 Flow 布局存在一些问题。我目前正在开发一个以流式布局显示帖子的应用程序,使其看起来既漂亮又时尚。但是,当在所有设备上测试应用程序时,我注意到显示发生了变化:

  • 第五代 iPad
  • iPad 空气
  • iPad 空气 2
  • iPad Pro(9.7 英寸)

但其余的 iPad 显示正常。这里似乎有什么问题?我目前正在使用此代码在 CollectionView 中设置我的布局样式:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        switch UIDevice().screenType {
        case .iPhoneX:
            return CGSize(width: 180.0, height: 180.0)
            break
        case .iPhone5:
            return CGSize(width: 152.0, height: 152.0)
            break
        case .iPhone6:
            return CGSize(width: 180.0, height: 180.0)
            break
        case .iPhone6Plus:
            return CGSize(width: 200.0, height: 200.0)
            break
        case .iPad:
            return CGSize(width: 400.0, height: 400.0)
            break
        case .iPadTwo:
            return CGSize(width: 400.0, height: 400.0)
            break
        case .iPadThree:
            return CGSize(width: 400.0, height: 400.0)
            break
        default:
            return CGSize(width: 200.0, height: 200.0)
            break
        }
    }

'iPad Two' 和 'iPad Three' 只是我尝试创建的一些枚举,以尝试确定 iPad 类型以及是否有输出打印一些文本(但它只是降为默认值),无论iPad。

这是我的屏幕尺寸:

 switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4
        case 1136:
            return .iPhone5
        case 1334:
            return .iPhone6
        case 2208:
            return .iPhone6Plus
        case 1024:
            return .iPad
        case 1366:
            return .iPadTwo
        case 834:
            return .iPadThree
        case 2436:
            return .iPhoneX
            break
        default:
            return .unknown
        }

这是我的问题的直观表示,出现在列出的设备上,但没有出现在其他设备上:

它应该是什么样子:

在所列设备上的外观:

如果有人可以帮助我解决此问题,我将非常感谢您的帮助,因为我似乎无法找到一个正常工作的解决方案。

【问题讨论】:

标签: ios swift ipad layout uicollectionview


【解决方案1】:

仅供参考。我建议,不要在sizeForItemAt indexPath中设置CollectionCell Size,尝试一些通用的方式。

你应该像这样扩展UICollectionViewFlowLayout 并创建新的布局,

class HomeLayout: UICollectionViewFlowLayout {    
    var numberOfItemsPerRow : Int {
        get{
            var value = 0
            if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
                value = 3
            } else {
                value = 5
            }
            return value
        }
        set {
            self.invalidateLayout()
        }
    }

    override func prepare() {
        super.prepare()
        if self.collectionView != nil {
            var newItemSize = self.itemSize
            let itemsPerRow = max(self.numberOfItemsPerRow, 1)
            let totalSpacing = self.minimumInteritemSpacing * CGFloat(itemsPerRow - 1)
            let newWidth = (self.collectionView!.bounds.size.width - self.sectionInset.left - self.sectionInset.right - totalSpacing) / CGFloat(itemsPerRow)
            newItemSize.width = max(newItemSize.width,newWidth)
            if self.itemSize.height > 0 {
                let aspectRatio: CGFloat = self.itemSize.width / self.itemSize.height
                newItemSize.height = newItemSize.width / aspectRatio
            }
            self.itemSize = newItemSize            
        }
    }
}

现在将您的 CollectionView 的 UICollectionViewFlowLayout 分配给 Interface builder 中的 HomeLayout

Storyboard 调整您的sectionInsetsMin Spacing

仅供参考。相应地在HomeLayout 中设置您的numberOfItemsPerRow

【讨论】:

  • 我会试试的。我试过后会给你反馈。谢谢。
  • 我仍然无法正常工作。也许是因为我没有设置 numberOfItemsPerRow?我该如何正确设置?
  • 假设 iPhone 需要每行 3 个项目,然后设置 value = 3 和 iPad 相应
  • 你不需要显式设置它,iPhone默认为3,iPad默认为5,如果你想显式设置你需要做一些代码
  • 像魅力一样工作,太棒了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2016-06-28
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多