Mar*_*ñuk 5 lazy-loading swift swiftui uiviewrepresentable
我正在尝试使用 UIViewRepresentable 实现延迟加载 MyLazyHStack 视图,但它不会延迟加载元素。我在 MyLazyHStack 的内容中使用 LazyHStack,但所有元素都是立即加载,而不是根据需要延迟加载。这是我的代码的一个最小示例。有人可以帮我弄清楚我做错了什么吗?
struct MyLazyHStack<Content: View>: UIViewRepresentable {
var content: Content
func makeUIView(context: Context) -> UIView {
let scrollView = UIScrollView()
scrollView.isPagingEnabled = true
let hostingController = UIHostingController(rootView: content)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(hostingController.view)
NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: scrollView.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
hostingController.view.heightAnchor.constraint(equalTo: scrollView.heightAnchor)
])
return scrollView
}
func updateUIView(_ uiView: UIView, context: Context) {}
}
struct ContentView: View {
var body: some View {
MyLazyHStack {
LazyHStack {
ForEach(1..<100) { index in
Text("Element \(index)")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |