Skip to content

Commit 67bfa3c

Browse files
Merge branch 'swiftui'
2 parents 7916101 + a189db0 commit 67bfa3c

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
@available(iOS 13.0, *)
1213
open class ContainerController: NSObject {
@@ -25,6 +26,8 @@ open class ContainerController: NSObject {
2526

2627
public var footerView: UIView?
2728

29+
public var hostingController: UIHostingController<AnyView>?
30+
2831
// MARK: Layout
2932

3033
public var layout: ContainerLayout = ContainerLayout()
@@ -452,6 +455,29 @@ open class ContainerController: NSObject {
452455
calculationViews()
453456
}
454457

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

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

0 commit comments

Comments
 (0)