From 30951413852c0f17620204c5326a927c386ab0ae Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 4 Jul 2024 16:10:17 +0800 Subject: [PATCH] updateUIView should check view.scrollView is not nil before makeProxy and it's better not to force unwrap scrollView --- Sources/LegacyScrollView/Proxy/LegacyScrollViewProxy.swift | 6 +++--- Sources/LegacyScrollView/Proxy/LegacyScrollViewReader.swift | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/LegacyScrollView/Proxy/LegacyScrollViewProxy.swift b/Sources/LegacyScrollView/Proxy/LegacyScrollViewProxy.swift index 273231f..91c99f0 100644 --- a/Sources/LegacyScrollView/Proxy/LegacyScrollViewProxy.swift +++ b/Sources/LegacyScrollView/Proxy/LegacyScrollViewProxy.swift @@ -10,14 +10,14 @@ import SwiftUI public struct LegacyScrollViewProxy { - internal var getScrollView: () -> UIScrollView + internal var getScrollView: () -> UIScrollView? internal var getRectOfContent: (_ id: Int) -> CGRect? internal var performScrollToPoint: (_ point: CGPoint, _ animated: Bool) -> Void internal var performScrollToId: (_ id: Int, _ anchor: UnitPoint, _ animated: Bool) -> Void internal var performScrollToIdIfNeeded: (_ id: Int, _ anchor: UnitPoint) -> Void /// Returns the UIScrollView - public var scrollView: UIScrollView { getScrollView() } + public var scrollView: UIScrollView? { getScrollView() } /// returns the content's CGRect public func rectOfContent(id: ID) -> CGRect? { getRectOfContent(id.hashValue) } /// performs a scroll to a specific `CGPoint` @@ -31,7 +31,7 @@ public struct LegacyScrollViewProxy { extension LegacyScrollViewReader { func makeProxy(with view: LegacyUIScrollViewReader) -> LegacyScrollViewProxy { LegacyScrollViewProxy { - view.scrollView! + view.scrollView } getRectOfContent: { id in getRectOfContent(with: id, in: view) } performScrollToPoint: { point, animated in diff --git a/Sources/LegacyScrollView/Proxy/LegacyScrollViewReader.swift b/Sources/LegacyScrollView/Proxy/LegacyScrollViewReader.swift index fbc1945..3343c5f 100644 --- a/Sources/LegacyScrollView/Proxy/LegacyScrollViewReader.swift +++ b/Sources/LegacyScrollView/Proxy/LegacyScrollViewReader.swift @@ -22,6 +22,10 @@ public struct LegacyScrollViewReader: UIViewRepresentable { } public func updateUIView(_ view: LegacyUIScrollViewReader, context: Context) { + guard let _ = view.scrollView else { + //Warning: updateUIView called before view was added to view hierarchy + return + } let proxy = makeProxy(with: view) let contentView = content(proxy)