Swiftpack.co - Priva28/HostingPassthrough as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Priva28.
Priva28/HostingPassthrough v1
Let SwiftUI and UIKit views live in harmony.
⭐️ 60
🕓 2 weeks ago
.package(url: "https://github.com/Priva28/HostingPassthrough.git", from: "v1")

HostingPassthrough

UIHostingController blocks all touches behind it. You basically have to choose SwiftUI, or UIKit? If you want a SwiftUI overlay over a UIKit view for example, you can't have that.

But SwiftUI and UIKit views deserve to live in harmony.

HostingPassthrough allows you to fix this by inheriting HostingParentController instead of UIViewController in the places where you will be adding SwiftUI views through UIHostingController.

image
  • If you don't want to force your UIHostingControllers to have clear backgrounds, set makeBackgroundsClear = false in viewDidLoad().
  • If you want to forward touches on the base view of the HostingParentController to another view, set forwardBaseTouchesTo to another UIView you want to handle your touches.

Some cool logic will then be applied overriding the hitTest method, which ignores any touches in parts of a UIHostingController that don't contain a SwiftUI view and pass it to whatever is underneath instead.

❇️ Update ❇️ : You can now inherit or initalise a HostingParentView instead of UIView, in the case that you aren't adding the UIHostingController to a parent view controller. While you should always add the UIHostingController to a parent view controller if possible to correctly manage view lifecycle, there may be instances where you are for example adding SwiftUI components to a reusable custom UIView in which case using HostingParentView would be acceptable.



All these views are tappable, even behind the 3 UIHostingController's!

Screenshot 2023-01-10 at 11 17 47 pm

https://user-images.githubusercontent.com/40876121/211558172-e02bb348-51f7-4a60-ae39-2dd3d29de06b.mov

GitHub

link
Stars: 60
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v1
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics