相关疑难解决方法(0)

SwiftUI 通用下拉刷新视图

我有这个 CustomScrollView,它包装了我的 HomeView,如果你拉下它,它会获取新数据。它工作正常,但问题是我想在多个视图中重用它,我不想为我的每个视图创建一个副本。我试图这样做,var rootView: View但它抛出一个错误说View is not convertible to HomeView

所以有两件事女巫应该是通用的。HomeView()HomeViewModel

知道如何实现这一目标吗?

struct CustomScrollView : UIViewRepresentable {
    var width : CGFloat
    var height : CGFloat

    let viewModel = HomeViewModel()

    func makeCoordinator() -> Coordinator {
        Coordinator(self, homeViewModel: viewModel)
    }

    func makeUIView(context: Context) -> UIScrollView {
        let control = UIScrollView()
        control.refreshControl = UIRefreshControl()
        control.refreshControl?.addTarget(context.coordinator, action: #selector(Coordinator.handleRefreshControl), for: .valueChanged)

        let childView = UIHostingController(rootView: HomeView())

        childView.view.frame = CGRect(x: 0, y: 0, width: width, height: height)

        control.addSubview(childView.view) …
Run Code Online (Sandbox Code Playgroud)

xcode ios swift swiftui

2
推荐指数
1
解决办法
2343
查看次数

标签 统计

ios ×1

swift ×1

swiftui ×1

xcode ×1