From d88ab78d6386560f72df6a31c64150e5e1b42d93 Mon Sep 17 00:00:00 2001 From: Stephen Heaps Date: Fri, 25 Oct 2024 11:27:47 -0400 Subject: [PATCH] Fix crash when dismissing NTP news opt-in. --- .../New Tab Page/NewTabPageFlowLayout.swift | 3 +++ .../NewTabPageViewController.swift | 23 +++---------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageFlowLayout.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageFlowLayout.swift index d6d0cdcdbfbe..50c1434dede7 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageFlowLayout.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageFlowLayout.swift @@ -64,6 +64,9 @@ class NewTabPageFlowLayout: UICollectionViewFlowLayout { lastSizedElementMinY = nil lastSizedElementPreferredHeight = nil + } else { + gapLength = 0 + extraHeight = 0 } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift index 43ec986d3439..d89a05943a0b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/New Tab Page/NewTabPageViewController.swift @@ -772,26 +772,9 @@ class NewTabPageViewController: UIViewController { collectionView.deleteItems(at: [IndexPath(item: 0, section: section)]) } - // We check if first item exists before scrolling up to it. - // This should never happen since first item is our shields stats view. - // However we saw it crashing in XCode logs, see #4202. - let firstItemIndexPath = IndexPath(item: 0, section: 0) - if let itemCount = collectionView.dataSource?.collectionView( - collectionView, - numberOfItemsInSection: 0 - ), - itemCount > 0, // Only scroll if the section has items, otherwise it will crash. - collectionView.dataSource? - .collectionView(collectionView, cellForItemAt: firstItemIndexPath) != nil - { - collectionView.scrollToItem(at: firstItemIndexPath, at: .top, animated: true) - } else { - // Cannot scorll to deleted item index. - // Collection-View datasource never changes or updates - // Therefore we need to scroll to offset 0. - // See: #4575. - collectionView.setContentOffset(.zero, animated: true) - } + // scroll to offset .zero to preserve padding above section + collectionView.setContentOffset(.zero, animated: true) + backgroundButtonsView.setNeedsLayout() collectionView.verticalScrollIndicatorInsets = .zero UIView.animate(withDuration: 0.25) { self.feedOverlayView.headerView.alpha = 0.0