|
- import SwiftUI
-
- @discardableResult
- public func addSwiftUIViewAsChild<Type>(
- swiftUIView: Type,
- parent: UIView
- ) -> UIHostingController<Type> {
- let childVC = UIHostingController(rootView: swiftUIView)
- childVC.view.backgroundColor = .clear
- parent.addSubview(childVC.view)
- childVC.view.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- childVC.view.topAnchor.constraint(equalTo: parent.topAnchor),
- childVC.view.bottomAnchor.constraint(equalTo: parent.bottomAnchor),
- childVC.view.leadingAnchor.constraint(equalTo: parent.leadingAnchor),
- childVC.view.trailingAnchor.constraint(equalTo: parent.trailingAnchor),
- ])
- return childVC
- }
|