I'm trying to use SwiftUI cells using the .contentConfiguration
APIs. However, the SwiftUI view that I'd like to use also needs to be able to display a popover. I'm able to present an alert, but not able to present a popover.
The following code uses a SwiftUIView
as the cell view that works to present an .alert
but does not work to present a .popover
. Note the .popover
does work in both Previews and when presented within a UIHostingController
.
import UIKit
import SwiftUI
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.contentConfiguration = UIHostingConfiguration { SwiftUIView(text: "Hello \(indexPath.row)") }
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
present(UIHostingController(rootView: SwiftUIView(text: "full screen")), animated: true)
}
}
struct SwiftUIView: View {
@State var presentAlert: Bool = false
@State var showPopover: Bool = false
let text: String
var body: some View {
HStack {
Text(text)
Button("Present Alert") { presentAlert = true }
Button("Present Popover") { showPopover = true }
}.popover(isPresented: $showPopover) {
Button("Hide Popover") {
showPopover = false
}
}.alert(isPresented: $presentAlert) {
Alert(title: Text("Alert"))
}
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView(text: "Hello")
}
}