简体   繁体   中英

SwiftUI - Presenting a ViewController/TableViewController from SwiftUI

The app is still heavily using ViewControllers and TableViewControllers, which is making it difficult to integrate SwiftUI.

Firstly, I'm presenting a swiftUI view from a ViewController.

class SwiftUIViewController : UIViewController {

    let contentView = UIHostingController(rootView: SwiftUIView())

    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.addSubview(contentView.view)
    }
}

Now, from within my SwiftUIView , I need to start ViewControllers and TableViewControllers. I created an injector class that can be used with.sheet to present a view controller from SwiftUI.

struct ViewControllerInjector : UIViewControllerRepresentable {

    let viewController: UIViewController

    func makeUIViewController(context: Context) -> UIViewController {
        return viewController
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    
    }
}

Now this kind of works, the only issue is that I can't seem to find a way to present the ViewController without it being a .sheet

struct SwiftUIView : View {
    @State private var showingVC1 = false
    ...

    .sheet(isPresented: $showingVC1) {
        ViewControllerInjector(viewController: ViewController1())
    }

    ...
}

I would like to completely leave the swift ui screen and continue with the view controller until I press the back button and return to the SwiftUI screen. Also, the injector doesn't seem to work with TableViewControllers even if I create a second injector class dedicated to TableView.

Is this type of navigation possible? If anything is not clear, don't hesitate to leave a comment. Thanks

Is the issue being that.sheet is a modal view and you would like it to be fullscreen? In that case, try replacing .sheet with .fullScreenCover

To present a TableViewController in SwiftUI, try switching UIViewController to UITableViewController or UITableView. Here's a code snippet I found here some time ago:

struct UITableViewRepresentable: UIViewRepresentable {

    func makeUIView(context: Context) -> UITableView {
   
        uiTableView.dataSource = context.coordinator
        uiTableView.delegate = context.coordinator

        }

        uiTableView.register(HostingCell.self, forCellReuseIdentifier: "Cell")
        return uiTableView
    }

    func updateUIView(_ uiTableView: UITableView, context: Context) {}

    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class HostingCell: UITableViewCell { // just to hold hosting controller
        var host: UIHostingController<AnyView>?
    }

    class Coordinator: NSObject, UITableViewDelegate, UITableViewDataSource {

        init(_ parent: UITableViewRepresentable) {
           
        }

        func numberOfSections(in tableView: UITableView) -> Int {
            return self.parent.items.keys.count
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return parent.items[section]?.count ?? 0
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           
          
            return tableViewCell
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM