From 8c3688708f3669ed68b26578c70e7f07cf25ade4 Mon Sep 17 00:00:00 2001 From: Bryan Keller Date: Thu, 14 Nov 2024 09:45:34 -0800 Subject: [PATCH] Improve SwiftUIWrapperView performance (#322) --- Sources/Public/ItemViews/SwiftUIWrapperView.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/Public/ItemViews/SwiftUIWrapperView.swift b/Sources/Public/ItemViews/SwiftUIWrapperView.swift index 92aaa3c..595f897 100644 --- a/Sources/Public/ItemViews/SwiftUIWrapperView.swift +++ b/Sources/Public/ItemViews/SwiftUIWrapperView.swift @@ -29,7 +29,7 @@ public final class SwiftUIWrapperView: UIView { public init(contentAndID: ContentAndID) { self.contentAndID = contentAndID - hostingController = UIHostingController(rootView: AnyView(contentAndID.content)) + hostingController = UIHostingController(rootView: contentAndID.content) hostingController._disableSafeArea = true super.init(frame: .zero) @@ -59,7 +59,9 @@ public final class SwiftUIWrapperView: UIView { public override var isHidden: Bool { didSet { if isHidden { - hostingController.rootView = AnyView(EmptyView()) + hostingControllerView.removeFromSuperview() + } else { + addSubview(hostingControllerView) } } } @@ -99,14 +101,14 @@ public final class SwiftUIWrapperView: UIView { fileprivate var contentAndID: ContentAndID { didSet { - hostingController.rootView = AnyView(contentAndID.content) + hostingController.rootView = contentAndID.content configureGestureRecognizers() } } // MARK: Private - private let hostingController: UIHostingController + private let hostingController: UIHostingController private var hostingControllerView: UIView { hostingController.view