SwiftUI-Introspect/Introspect/UIKitIntrospectionViewContr...

54 lines
1.9 KiB
Swift

#if canImport(UIKit)
import SwiftUI
import UIKit
/// Introspection UIViewController that is inserted alongside the target view controller.
public class IntrospectionUIViewController: UIViewController {
required init() {
super.init(nibName: nil, bundle: nil)
view = IntrospectionUIView()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
/// This is the same logic as IntrospectionView but for view controllers. Please see details above.
public struct UIKitIntrospectionViewController<TargetViewControllerType: UIViewController>: UIViewControllerRepresentable {
let selector: (IntrospectionUIViewController) -> TargetViewControllerType?
let customize: (TargetViewControllerType) -> Void
public init(
selector: @escaping (UIViewController) -> TargetViewControllerType?,
customize: @escaping (TargetViewControllerType) -> Void
) {
self.selector = selector
self.customize = customize
}
public func makeUIViewController(
context: UIViewControllerRepresentableContext<UIKitIntrospectionViewController>
) -> IntrospectionUIViewController {
let viewController = IntrospectionUIViewController()
viewController.accessibilityLabel = "IntrospectionUIViewController<\(TargetViewControllerType.self)>"
viewController.view.accessibilityLabel = "IntrospectionUIView<\(TargetViewControllerType.self)>"
return viewController
}
public func updateUIViewController(
_ uiViewController: IntrospectionUIViewController,
context: UIViewControllerRepresentableContext<UIKitIntrospectionViewController>
) {
DispatchQueue.main.async {
guard let targetView = self.selector(uiViewController) else {
return
}
self.customize(targetView)
}
}
}
#endif