Skip to content

Commit a189db0

Browse files
Preliminary SwiftUI support
For now it will simply add a `UIHostingController` `view` to the `contentView`. I commented out calls needed for proper view controller containment because it doesn’t play well with my `UINavigationController`.
1 parent aa2f484 commit a189db0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/ContainerController/ContainerController.swift

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import UIKit
10+
import SwiftUI
1011

1112
open class ContainerController: NSObject {
1213

@@ -24,6 +25,8 @@ open class ContainerController: NSObject {
2425

2526
public var footerView: UIView?
2627

28+
public var hostingController: UIHostingController<AnyView>?
29+
2730
// MARK: Layout
2831

2932
public var layout: ContainerLayout = ContainerLayout()
@@ -451,6 +454,29 @@ open class ContainerController: NSObject {
451454
calculationViews()
452455
}
453456

457+
// MARK: - Add SwiftUI View
458+
459+
public func removeSwiftUIView() {
460+
// self.hostingController?.willMove(toParent: nil)
461+
self.hostingController?.view.removeFromSuperview()
462+
// self.hostingController?.removeFromParent()
463+
self.hostingController = nil
464+
}
465+
466+
public func add<V: View>(swiftUIView: V) {
467+
guard let contentView = self.view.contentView else {
468+
return
469+
}
470+
removeSwiftUIView()
471+
let hostingController = UIHostingController(rootView: AnyView(swiftUIView))
472+
self.hostingController = hostingController
473+
// self.controller?.addChild(hostingController)
474+
hostingController.view.frame = contentView.bounds
475+
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
476+
contentView.addSubview(hostingController.view)
477+
// hostingController.didMove(toParent: self.controller)
478+
}
479+
454480
// MARK: - Pan Gesture
455481

456482
@objc private func handlePan(_ gesture: UIPanGestureRecognizer) {

0 commit comments

Comments
 (0)