-
-
Save alongotv/7f450e8c47ed3f057e1f6d35443af269 to your computer and use it in GitHub Desktop.
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!
@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 ;)
Just stumbled upon this discussion and I think that this might be related: feedback-assistant/reports#411
Had an issue where it wasn't recognizing the ViewModifier as valid, changed some stuff around and now it works:
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 ?? {})()
}
}
}
extension View {
func onDidAppear(_ perform: @escaping () -> Void) -> some View {
return background(ViewControllerLifecycleHandler(onDidAppear: perform))
}
}
Hope this helps anyone else trapped on older versions of iOS!
Yeah, sure... @alongotv
Where u get this log:
As you can see, the 3 view in the TabView is created before it even appears...