PullToRefreshView在CardViews上重叠

问题描述

我已经实现了PullToRefreshView,它将刷新来自远程API的所有数据。 它的作用是使用此方法 .removeAll()删除所有数据,然后再次从远程API加载所有数据,并使用此方法将Refreshcontrol设置为off。 endRefreshing()

问题是当我实现此PullToRefreshView时,它涵盖了一些视图。

如您在此图中看到的,PullToRefreshView与某些CardView重叠

Image

我认为我的问题在于RefreshView的大小

所以请帮助

这是我的RefreshController

import Foundation
import SwiftUI

struct refreshcontent:  UIViewRepresentable {
    
    @Binding var refreshcontrol: UIRefreshControl
    
    @Observedobject var Datasets =  NetworkingManager()
    func makeCoordinator() -> Coordinator {
        return refreshcontent.Coordinator()
    }
    
    func makeUIView(context: Context) -> UIScrollView {
        let view = UIScrollView()
        let height =  CGFloat((400 * self.Datasets.items.count) + 15 * Datasets.items.count * 2) + 20
        view.showsverticalScrollIndicator = false
        view.refreshControl = refreshcontrol
        refreshcontrol.attributedTitle = NSAttributedString(string: "Pull to refresh")
        refreshcontrol.addTarget(context.coordinator,action: #selector(context.coordinator.update),for: .valueChanged)
        view.contentSize = CGSize(width: UIScreen.main.bounds.width,height:  height)
        let child = UIHostingController(rootView: MainLoadingView(refreshcontrol: self.$refreshcontrol))
        child.view.frame = CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: height)
        child.view.backgroundColor = .clear
        view.addSubview(child.view)
        return view
        
    }
    func updateUIView(_ uiView: UIScrollView,context: Context) {
        let height =  CGFloat((400 * self.Datasets.items.count) + 15 * Datasets.items.count * 2) + 20
        uiView.contentSize = CGSize(width: UIScreen.main.bounds.width,height:  height)
        for i in 0..<uiView.subviews.count {
            uiView.subviews[i].frame = CGRect(x: 0,height:  height)}
    }
    
    class Coordinator: NSObject {
        @objc func update() {
            NotificationCenter.default.post(name: NSNotification.Name("update"),object: nil)
        }
    }
}

在上面的代码中,请注意恒定高度,因为我y =认为这可能是原因。

我的卡视图

struct browsePostView: View {
    var deta: contents
    var body: some View {
        if deta.thumbnail?.source != nil {
            vstack(alignment: .leading) {
                ImageView(urltostring: deta.thumbnail?.source)
                Text(deta.title!.capitalized)
                    .foregroundColor(.primary)
                    .font(.headline)
                    .padding([.leading])
                Text(deta.extract ?? "no discription found,please try again later...")
                    .font(.subheadline)
                    .lineLimit(5)
                    .foregroundColor(.secondary)
                    .padding([.leading,.bottom,.trailing])
            }.frame(height: 352).background(Color(.secondarySystemBackground))
            .cornerRadius(10.0)
            .shadow(radius: 10.0)
            .padding(.all)
        }
    }
}

我的主视图

struct MainLoadingView: View  {
    @Observedobject var datasets = NetworkingManager()
    @Binding var refreshcontrol: UIRefreshControl
    var body: some View {
        if datasets.Loadingerror == .loadded {
            ForEach(datasets.items,id:\.pageid) { page in
                vstack(alignment: .leading){
                    NavigationLink(destination: DetailView(Detaildata: page)) {
                        withAnimation {
                            browsePostView(deta: page)
                        }
                    }
                }
                .onAppear {
                    if !self.datasets.endOfList {
                        if self.datasets.shouldLoadMore(articleItem: page) {
                            self.datasets.loadmorecontents()
                        }
                    }
                    NotificationCenter.default.addobserver(forName: NSNotification.Name("update"),object: nil,queue: .main) { (_) in
                        dispatchQueue.main.asyncAfter(deadline: .Now() + 0.5) {
                            self.datasets.items.removeAll()
                            self.datasets.loadmorecontents()
                            self.refreshcontrol.endRefreshing()
                        }
                    }
                }
            }
        } else if datasets.Loadingerror == .loading {
            HStack(spacing: 10) {
                ProgressView()
                Text("Loading please wait!")
            }
        } else
        {
            Text("hellow")
        }
    }
}

我的最终通话视图

struct CheakingList: View {
    @Observedobject var datasets = NetworkingManager()
    @State var refreshcontrol = UIRefreshControl()
    var body: some View {
       
        NavigationView {
            vstack {
                refreshcontent(refreshcontrol: self.$refreshcontrol)
            }.navigationBarTitle("browse")
    }
}
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)