Skip to content

Commit

Permalink
set accessibility identifier for debugging (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 15, 2024
1 parent 7e8eaca commit 2113b26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 62 deletions.
82 changes: 20 additions & 62 deletions Sources/SwiftUIHosting/HostingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,36 @@ final class HostingController<Content: View>: UIHostingController<Content> {

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
14 changes: 14 additions & 0 deletions Sources/SwiftUIHosting/SwiftUIHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2113b26

Please sign in to comment.