相关疑难解决方法(0)

SwiftUI 可刷新(拉动刷新)在 ScrollView 上不起作用

var body: some View {
    NavigationView {
        ZStack {
            Color(UIColor.systemGroupedBackground).edgesIgnoringSafeArea(.all)
            ScrollView(showsIndicators: false) {
                if #available(iOS 15.0, *) {
                    VStack {
                        if self.viewModel.forms.isEmpty && !self.viewModel.isLoading {
                            Text("No Forms Assigned")
                                .foregroundColor(Color.gray)
                                .padding(.vertical, 20)
                                .accessibility(label: Text("No forms assigned"))
                        }
                        if self.viewModel.isLoading {
                            ActivityIndicator(isAnimating: .constant(true), style: .large).padding(.top, 20)
                        }
                        noFormsScrollView
                        Spacer(minLength: 16).accessibility(hidden: true)
                    }
                    .refreshable {
                        self.refreshData()
                    }
                } else {
                    // Fallback on earlier versions
                }
            }
        }.navigationBarTitle("Forms", displayMode: .inline)
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试添加拉动以刷新我的ScrollView但它不起作用。我想知道我做错了什么。

swift swiftui

5
推荐指数
2
解决办法
2万
查看次数

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
查看次数

标签 统计

swift ×2

swiftui ×2

ios ×1

xcode ×1