ios – SwiftUI `.popover` does not work inside UIHostingConfiguration cell displayed in a UITableViewController?


I might like to make use of SwiftUI cells utilizing the .contentConfiguration APIs. Nevertheless, the SwiftUI view that I might like to make use of additionally wants to have the ability to show a popover. I can current an alert, however not in a position to current a popover.

Is there a means to do that utilizing the .contentConfiguration API or do I have to show the SwiftUI cells in one other method. Finally, I will refactor the UITableViewController into Swift however for now I might like to begin introducing SwiftUI piece by piece.

The next reveals .popover not working throughout the UIHostingConfiguration however working when offered inside a UIHostingController.

import UIKit
import SwiftUI

class ViewController: UITableViewController {
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        3
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        cell.contentConfiguration = UIHostingConfiguration { SwiftUIView(textual content: "Hiya (indexPath.row)") }

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        current(UIHostingController(rootView: SwiftUIView(textual content: "full display")), animated: true)
    }
}

struct SwiftUIView: View {
    @State var presentAlert: Bool = false
    @State var showPopover: Bool = false

    let textual content: String

    var physique: some View {
        HStack {
            Textual content(textual content)
            Button("Current Alert") { presentAlert = true }
            Button("Current Popover") { showPopover = true }
        }.popover(isPresented: $showPopover) {
            Button("Conceal Popover") {
                showPopover = false
            }
        }.alert(isPresented: $presentAlert) {
            Alert(title: Textual content("Alert"))
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView(textual content: "Hiya")
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles