From 3e858c652fed1f062495e2488179277b78e19f47 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 16 Jun 2023 15:25:49 -0500 Subject: [PATCH] Fixes crash loading save states on iOS 17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Crashes due to GridCollectionViewLayout returning outdated cached layout information when inserting “Auto” save states section for the first time. To fix this, we now clear the layout cache in invalidateLayout(with:) when relevant. --- .../Collection View/GridCollectionViewLayout.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Delta/Components/Collection View/GridCollectionViewLayout.swift b/Delta/Components/Collection View/GridCollectionViewLayout.swift index 4241ad2..0b34efe 100644 --- a/Delta/Components/Collection View/GridCollectionViewLayout.swift +++ b/Delta/Components/Collection View/GridCollectionViewLayout.swift @@ -66,6 +66,18 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout self.sectionInset.right = self.interitemSpacing + self.contentInset.right } + override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext) + { + super.invalidateLayout(with: context) + + if let context = context as? UICollectionViewFlowLayoutInvalidationContext, + context.invalidateFlowLayoutAttributes || context.invalidateFlowLayoutDelegateMetrics || context.invalidateEverything + { + // We must clear layout cache on iOS 17 or later to prevent crashing due to returning outdated layout attributes. + self.cachedLayoutAttributes = [:] + } + } + override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let layoutAttributes = super.layoutAttributesForElements(in: rect)?.map({ $0.copy() }) as! [UICollectionViewLayoutAttributes]