【发布时间】:2019-11-03 00:14:04
【问题描述】:
我正在尝试在 SwiftUI 中开发我的第一个 IOS 应用程序,我想要下拉刷新列表。我已经知道目前 Apple 还没有实现它,但我在 stackoverflow (Pull down to refresh data in SwiftUI) 上找到了以下解决方案,我从第一个答案中实现了解决方案。这很好用。
但现在我想在这个可刷新的视图中拥有一个 SwiftUI 视图。解决方案说我必须:
将它们包装在 UIHostingController 中并将它们放入 makeUIView
这是我的问题。我到底要做什么?我尝试了以下方法,但它不起作用。
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
}
有谁知道我做错了什么并且可以告诉我我必须改变什么?
感谢和问候
亨里克
【问题讨论】:
-
您还没有将任何控件添加到 UIScrollView
control,因此它们都不会出现。您应该能够做到control.addChild(child)并完全删除parent。做出改变,看看它是否有效。如果没有,了解更多关于它如何不工作的细节会很有帮助。 -
感谢您的回复。但是当我尝试这个时,我只会收到以下错误:
Value of type 'UIScrollView' has no member 'addChild'。我正在使用来自其他页面的代码,目前我尝试添加一个简单的文本:let child = UIHostingController(rootView: Text("Hello"))。这是我在makeUIViewfunction 中添加的。其他所有内容仅从其他帖子中复制
标签: swift list swiftui uirefreshcontrol