Created
February 26, 2025 12:01
-
-
Save okwasniewski/eadcc7a1dd58abab67c3a0bf29f0f96a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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