Swiftui 如何在navigationView中补充UIScrollView?

Swiftui 如何在navigationView中补充UIScrollView?,swiftui,swiftui-navigationview,Swiftui,Swiftui Navigationview,我目前正在使用SwiftUI开发一个应用程序,并尝试使用pull操作刷新数据 当我在列表中实现该函数时,它可以工作,但如果我在导航视图中使用该函数,则该函数不工作 // ---OK↓--- RefreshScrollViewTest(refreshControl: self.$refreshControl) // ---NG↓--- NavigationView{ RefreshScrollViewTest(refreshControl: self.$refreshControl) }

我目前正在使用SwiftUI开发一个应用程序,并尝试使用pull操作刷新数据

当我在
列表
中实现该函数时,它可以工作,但如果我在
导航视图
中使用该函数,则该函数不工作

// ---OK↓---
RefreshScrollViewTest(refreshControl: self.$refreshControl)

// ---NG↓---
NavigationView{
    RefreshScrollViewTest(refreshControl: self.$refreshControl)
}
是否有任何方法可以使用
NavigationView
中的功能


代码如下:

import SwiftUI

struct NavigationRefreshTest: View {

    @State var refreshControl = UIRefreshControl()

    var body: some View {
        //        NavigationView{
        RefreshScrollViewTest(refreshControl: self.$refreshControl)
        //        }
    }
}


struct RefreshListTest:View {

    @Binding var refreshControl:UIRefreshControl

    var body: some View{
            List{
                Text("test1")
                Text("test2")
                Text("test3")
            }
        .onAppear{
            NotificationCenter.default.addObserver(forName: NSNotification.Name("Update"), object: nil, queue: .main){ (_) in
                DispatchQueue.main.asyncAfter(deadline: .now() + 1){
                    print("update...")
                }
            }
        }
    }
}


struct RefreshScrollViewTest:UIViewRepresentable {

    func makeCoordinator() ->Coodinator {
        return RefreshScrollViewTest.Coodinator()
    }

    @Binding var refreshControl:UIRefreshControl

    func makeUIView(context: Context) -> UIScrollView {
        let view = UIScrollView()

        self.refreshControl.attributedTitle = NSAttributedString(string: "Loding")
        self.refreshControl.addTarget(context.coordinator, action: #selector(context.coordinator.update), for: .valueChanged)

        view.showsVerticalScrollIndicator = false
        view.refreshControl = self.refreshControl
        view.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

        let child = UIHostingController(rootView: RefreshListTest(refreshControl: self.$refreshControl))

        child.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width,
                                  height: UIScreen.main.bounds.height)
        view.addSubview(child.view)

        return view
    }

    func updateUIView(_ uiView: UIScrollView, context: Context) {
    }

    class Coodinator:NSObject{
        @objc func update(){
            NotificationCenter.default.post(name: NSNotification.Name("Update"), object: nil)
        }
    }
}

Xcode:12.3版


iOS:14.0

我还没有解决您的问题,但您应该使用此库

我还没有解决您的问题,但您应该使用此库

谢谢您的支持,这是一种简单且有用的方法!在我的例子中,我使用了一些解决方案()谢谢你的支持,这是一个简单的方法和帮助!在我的例子中,我使用了一些解决方案()