diff --git a/Sources/SwiftUIHosting/HostingController.swift b/Sources/SwiftUIHosting/HostingController.swift index f5c7156..b3c26b2 100644 --- a/Sources/SwiftUIHosting/HostingController.swift +++ b/Sources/SwiftUIHosting/HostingController.swift @@ -5,78 +5,36 @@ final class HostingController: UIHostingController { var onViewDidLayoutSubviews: (UIViewController) -> Void = { _ in } - init(disableSafeArea: Bool, rootView: Content) { - super.init(rootView: rootView) + private let accessibilityIdentifier: String? + + init( + accessibilityIdentifier: String? = nil, + disableSafeArea: Bool, + rootView: Content + ) { - // https://www.notion.so/muukii/UIHostingController-safeArea-issue-ec66a560970c4a1cb44f21cc448bc513?pvs=4 -#if USE_SWIZZLING - _ = _once_ - _fixing_safeArea = disableSafeArea -#else + self.accessibilityIdentifier = accessibilityIdentifier + + super.init(rootView: rootView) + + // waiting for iOS 16.4 as minimum deployment target _disableSafeArea = disableSafeArea -#endif + } - + @MainActor required dynamic init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() + override func viewDidLoad() { + super.viewDidLoad() - onViewDidLayoutSubviews(self) + view.accessibilityIdentifier = accessibilityIdentifier } -} - -#if USE_SWIZZLING - -private let _once_: Void = { - UIView.replace() -}() - -private var _key: Void? - -extension UIView { - fileprivate static func replace() { - - method_exchangeImplementations( - class_getInstanceMethod(self, #selector(getter:UIView.safeAreaInsets))!, - class_getInstanceMethod(self, #selector(getter:UIView._hosting_safeAreaInsets))! - ) - - method_exchangeImplementations( - class_getInstanceMethod(self, #selector(getter:UIView.safeAreaLayoutGuide))!, - class_getInstanceMethod(self, #selector(getter:UIView._hosting_safeAreaLayoutGuide))! - ) - - } - - fileprivate var _fixing_safeArea: Bool { - get { - (objc_getAssociatedObject(self, &_key) as? Bool) ?? false - } - set { - objc_setAssociatedObject(self, &_key, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC) - } - } - - @objc dynamic var _hosting_safeAreaInsets: UIEdgeInsets { - if _fixing_safeArea { - return .zero - } else { - return self._hosting_safeAreaInsets - } - } + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() - @objc dynamic var _hosting_safeAreaLayoutGuide: UILayoutGuide? { - if _fixing_safeArea { - return nil - } else { - return self._hosting_safeAreaLayoutGuide - } + onViewDidLayoutSubviews(self) } - } - -#endif diff --git a/Sources/SwiftUIHosting/SwiftUIHostingView.swift b/Sources/SwiftUIHosting/SwiftUIHostingView.swift index 8dd9332..5f05740 100644 --- a/Sources/SwiftUIHosting/SwiftUIHostingView.swift +++ b/Sources/SwiftUIHosting/SwiftUIHostingView.swift @@ -63,11 +63,25 @@ open class SwiftUIHostingView: UIView { self.configuration = configuration + #if DEBUG + + self.hostingController = HostingController( + accessibilityIdentifier: _typeName(Content.self), + disableSafeArea: configuration.disableSafeArea, + rootView: AnyView(content()) + ) + + #else + self.hostingController = HostingController( disableSafeArea: configuration.disableSafeArea, rootView: AnyView(content()) ) + #endif + + + super.init(frame: .null) #if DEBUG