I am trying to develop my first IOS app in SwiftUI and I want to have a pull down to refresh List. I already know that at the moment Apple hasn't implemented it but I found the following solution here on stackoverflow (Pull down to refresh data in SwiftUI) and I am implemented the solution from the first answer. And this works fine.
But now I want to have a SwiftUI View in this refreshable View. The solution says I have to:
wrapping them in a UIHostingController and dropping them in makeUIView
And here is my problem. What did I have to do exactly? I tried the following but it isn't working.
func makeUIView(context: Context) -> UIScrollView {
let control = UIScrollView()
control.refreshControl = UIRefreshControl()
control.refreshControl?.addTarget(context.coordinator, action:
#selector(Coordinator.handleRefreshControl),
for: .valueChanged)
// Simply to give some content to see in the app
/*let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
label.text = "Scroll View Content"
control.addSubview(label)*/
let parent = UIViewController()
let child = UIHostingController(rootView: RecipeList())
child.view.translatesAutoresizingMaskIntoConstraints = false
child.view.frame = parent.view.bounds
// First, add the view of the child to the view of the parent
parent.view.addSubview(child.view)
// Then, add the child to the parent
parent.addChild(child)
return control
}
Has anyone an idea what I am doing wrong and can tell me what I have to change?
Thanks and best regards
Henrik