Skip to content

Instantly share code, notes, and snippets.

@okwasniewski
Created February 26, 2025 12:01
Show Gist options
  • Save okwasniewski/eadcc7a1dd58abab67c3a0bf29f0f96a to your computer and use it in GitHub Desktop.
Save okwasniewski/eadcc7a1dd58abab67c3a0bf29f0f96a to your computer and use it in GitHub Desktop.
import UIKit
import React
import SwiftUI
@objc public class StoreViewProvider: UIView {
private var props = Props()
private var hostingController: UIHostingController<SwiftUIStoreView>?
public override func layoutSubviews() {
super.layoutSubviews()
setupView()
}
private func setupView() {
if self.hostingController != nil {
return
}
self.hostingController = UIHostingController(
rootView: SwiftUIStoreView(
props: props
)
)
if let hostingController = self.hostingController {
addSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.pinEdges(to: self)
reactAddController(toClosestParent: hostingController)
}
}
}
extension UIView {
func pinEdges(to other: UIView) {
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: other.leadingAnchor),
trailingAnchor.constraint(equalTo: other.trailingAnchor),
topAnchor.constraint(equalTo: other.topAnchor),
bottomAnchor.constraint(equalTo: other.bottomAnchor)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment