Last active
February 10, 2023 17:21
-
-
Save alongotv/7f450e8c47ed3f057e1f6d35443af269 to your computer and use it in GitHub Desktop.
Adds viewDidAppear callback to SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ViewControllerLifecycleHandler: UIViewControllerRepresentable { | |
func makeCoordinator() -> ViewControllerLifecycleHandler.Coordinator { | |
Coordinator(onDidAppear: onDidAppear) | |
} | |
let onDidAppear: () -> Void | |
func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerLifecycleHandler>) -> UIViewController { | |
context.coordinator | |
} | |
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<ViewControllerLifecycleHandler>) { | |
} | |
typealias UIViewControllerType = UIViewController | |
class Coordinator: UIViewController { | |
let onDidAppear: (() -> Void)? | |
init(onDidAppear: (() -> Void)?) { | |
self.onDidAppear = onDidAppear | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
(onDidAppear ?? {})() | |
} | |
} | |
} | |
struct DidAppearModifier: ViewModifier { | |
let onDidAppearCallback: (() -> Void)? | |
func body(content: Content) -> some View { | |
content | |
.background(ViewControllerLifecycleHandler(onDidAppear: onDidAppearCallback ?? {})) | |
} | |
} | |
extension View { | |
func onDidAppear(_ perform: @escaping () -> Void) -> some View { | |
self.modifier(DidAppearModifier(onDidAppearCallback: perform)) | |
} | |
} |
@Alexminator99 It looks like something might be broken in TabView once again - I can't receive viewDidAppear callback in the UIViewControllerRepresentable if applied to View inside the TabView (iOS 16.1.1 physical device). Although, viewWillAppear works just fine. Unfortunately, I was unable to find a workaround, other than using that modifier outside the TabView or putting the TabView's child into a ".sheet{}". Either way, it's a pity that this gist wasn't helpful for you. Sorry!
I already reported the issue. Thks anyway, the gist is really interesting ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Alexminator99 It looks like something might be broken in TabView once again - I can't receive viewDidAppear callback in the UIViewControllerRepresentable if applied to View inside the TabView (iOS 16.1.1 physical device). Although, viewWillAppear works just fine. Unfortunately, I was unable to find a workaround, other than using that modifier outside the TabView or putting the TabView's child into a ".sheet{}". Either way, it's a pity that this gist wasn't helpful for you. Sorry!