【发布时间】:2020-10-11 12:45:19
【问题描述】:
我已经在 SwiftUI 中使用 UIViewRepresentable 实现了我的自定义 UIScrollView,我想检测列表的底部,以便加载下一批数据。我找到了一些答案,但它们似乎不适用于我的UIViewRepresentable。
我已经在我的协调器中实现了这个,但是当我到达底部时没有任何反应。
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
print("bottom reached")
// not working
}
}
问题可能出在我的 Swift 视图中,而不是在 UIViewRepresentable.?
struct CustomScrollView<Content: View, VM: LoadProtocol> : UIViewRepresentable {
var width : CGFloat
var height : CGFloat
var viewModel: VM
let content: () -> Content
func makeCoordinator() -> Coordinator {
Coordinator(self, viewModel: 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: content())
childView.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
control.addSubview(childView.view)
return control
}
func updateUIView(_ uiView: UIScrollView, context: Context) { }
class Coordinator: NSObject {
var control: CustomScrollView<Content, VM>
var viewModel: VM
init(_ control: CustomScrollView, viewModel: VM) {
self.control = control
self.viewModel = viewModel
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
print("bottom reached")
// not working
}
}
@objc func handleRefreshControl(sender: UIRefreshControl) {
sender.endRefreshing()
viewModel.refresh()
}
}
}
struct SeeAllView: View {
@ObservedObject var seeAllViewModel: SeeAllViewModel
var body: some View {
VStack {
GeometryReader { geometry in
CustomScrollView(width: geometry.size.width, height: geometry.size.height, viewModel: self.seeAllViewModel) {
SeeAllListView(seeAllViewModel: self.seeAllViewModel)
}
}
}
}
}
【问题讨论】:
标签: ios swift uiscrollview swiftui