Ios 具有不同单元格类型的集合视图:当方向改变时,如何调整其中一些单元格的大小?

Ios 具有不同单元格类型的集合视图:当方向改变时,如何调整其中一些单元格的大小?,ios,uicollectionview,rotation,orientation,uicollectionviewcell,Ios,Uicollectionview,Rotation,Orientation,Uicollectionviewcell,我已注册了两种不同的自定义UICollectionViewCell类型: collectionView.register(CustomCell1.self, forCellWithReuseIdentifier: "cell1") collectionView.register(CustomCell2.self, forCellWithReuseIdentifier: "cell2") 对于其中一种类型的单元格,我需要它的纵向大小与横向大小不同(我有两个部分,每个部分都填充了一种类型的单元格)

我已注册了两种不同的自定义
UICollectionViewCell
类型:

collectionView.register(CustomCell1.self, forCellWithReuseIdentifier: "cell1")
collectionView.register(CustomCell2.self, forCellWithReuseIdentifier: "cell2")
对于其中一种类型的单元格,我需要它的纵向大小与横向大小不同(我有两个部分,每个部分都填充了一种类型的单元格),我实现了
collectionView(uu:layout:sizeForItemAt:)
如下:

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.section == 0 {
        return CGSize(width: 30, height: 30)
    }

    if UIInterfaceOrientationIsLandscape(UIApplication.shared.statusBarOrientation) {
        return CGSize(width: 300, height: 300)
    }

    return CGSize(width: 100, height: 100)
}
人像截图:

景观截图:

这似乎适用于iOS 11(当我旋转设备时,橙色单元格会调整大小),但我在
Xcode
中收到一些奇怪的消息,如下所示:

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.section == 0 {
        return CGSize(width: 30, height: 30)
    }

    if UIInterfaceOrientationIsLandscape(UIApplication.shared.statusBarOrientation) {
        return CGSize(width: 300, height: 300)
    }

    return CGSize(width: 100, height: 100)
}
[App]如果我们在真正的预提交处理程序中,由于CA限制,我们实际上无法添加任何新的防护

另一方面,这在iOS 10上根本不起作用(橙色单元格不调整大小)


处理这种情况的正确方法是什么?

您是否尝试过SizeClass?请查看