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但它不起作用。我想知道我做错了什么。
我有这个 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)