【发布时间】:2020-04-05 01:09:16
【问题描述】:
我是 SwiftUI 新手,我需要在 SwiftUI 应用程序中使用水平 UIScrollView,但是当我尝试包装它时,我无法让它正确呈现视图。我放在里面的视图就消失了。有没有办法将包装好的 UIScrollView 修复为仅水平滚动?
这是我的尝试。
struct CustomScrollView<Content>: UIViewRepresentable where Content : View {
let content: () -> Content
init(proxy: ListScrollingProxy, @ViewBuilder content: @escaping () -> Content) {
self.content = content
}
func makeUIView(context: Context) -> UIView {
let hosting = UIHostingController(rootView: content())
let width = UIScreen.main.bounds.width
let size = hosting.view.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
hosting.view.frame = CGRect(x: 0, y: 0, width: width, height: size.height)
let scrollview = UIScrollView()
scrollview.showsVerticalScrollIndicator = false
scrollview.showsHorizontalScrollIndicator = false
scrollview.addSubview(hosting.view)
scrollview.contentSize = CGSize(width: width, height: size.height)
return scrollview
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
我们将不胜感激。
【问题讨论】:
-
In this post你可以找到已经解决的垂直滚动UIScrollView集成,所以你只需要为水平滚动正确配置它。