0

I'm using the below PullToRefreshHack from this SO Answer which works well, but the problem is when I wrap it inside a NavigationView (which I need to be able to show this view's large navigation title) I loose the functionality of the pull to refresh. I'm not sure why though? How can I fix this, or remove the NavigationView but still show a large title at the top?

 //Usage
     var body: some View {
        NavigationView {
            ScrollView {
             PullToRefreshHack(coordinateSpaceName: "pullToRefreshInTrendsView") {
                        print("user pulled to refresh")
                          generator.impactOccurred()
                        self.loadDataForTrendsView()
                    }


struct PullToRefreshHack: View {
    
    var coordinateSpaceName: String
    var onRefresh: ()->Void
    
    @State var needRefresh: Bool = false
    
    var body: some View {
        GeometryReader { geo in
            if (geo.frame(in: .named(coordinateSpaceName)).midY > 50) {
                Spacer()
                    .onAppear {
                        needRefresh = true
                    }
            } else if (geo.frame(in: .named(coordinateSpaceName)).maxY < 10) {
                Spacer()
                    .onAppear {
                        if needRefresh {
                            needRefresh = false
                            onRefresh()
                        }
                    }
            }
            HStack {
                Spacer()
                if needRefresh {
                    ProgressView()
                } else {
                    Text("")
                }
                Spacer()
            }
            .onAppear {
                //print("PullToRefreshHack VIEW .onAppear is called")
            }
        }.padding(.top, -50)
    }
    
}
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • Have you tried experimenting with the value after `.midY` and `.maxY`? – Yotzin Castrejon Apr 02 '21 at 22:07
  • @YotzinCastrejon I'm not sure I follow why that would affect this? – GarySabo Apr 02 '21 at 22:37
  • I'm unsure on how to duplicate your issue of losing functionality. So what I assumed is when you use `.navigationBarTitle("Text")` in the future, you may not like that it was in the way of your pull to refresh. So I thought that because it was in the way, you may have meant that you don't get functionality any longer because the large title was in the way. That's why I mentioned you could move the `geo.frame` limits in your PullToRefreshHack View. Also, removing the -50 padding from PullToRefreshHack helps space it out better. Sorry if I misunderstood. – Yotzin Castrejon Apr 02 '21 at 22:47
  • @YotzinCastrejon thank you, its not that it is in the way...its something about being wrapped in the NavigationView, that my Pull to Refresh doesn't get called, e.g. in the code above ` print("user pulled to refresh")` never prints, if I remove the NavigationView then it does. – GarySabo Apr 05 '21 at 19:35

0 Answers0