[英]How to wrap a horizontal UIScrollView so it works for SwiftUI
我是 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) {
}
}
任何幫助將不勝感激。
在我的例子中,我包裝的視圖包含一個 WKWebView。 所以我關閉滾動,因為 SwiftUI 視圖處理滾動:
wrappedView.webView.scrollView.isScrollEnabled = false
此外,您需要確保您的視圖返回正確的intrinsicContentSize
值 - 視圖的完整大小,因此不需要滾動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.