Skip to content

Instantly share code, notes, and snippets.

@mishimay
Created July 15, 2019 05:50
Show Gist options
  • Save mishimay/81beb17814949a9305ad4f5022a38ae5 to your computer and use it in GitHub Desktop.
Save mishimay/81beb17814949a9305ad4f5022a38ae5 to your computer and use it in GitHub Desktop.
Bridge UIScrollView to SwiftUI
struct MyScrollView: UIViewRepresentable {
let swiftUIView: AnyView
func makeUIView(context: UIViewRepresentableContext<ContentView.MyScrollView>) -> UIView {
let hosting = UIHostingController(rootView: swiftUIView)
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 view = UIScrollView()
view.alwaysBounceVertical = true
view.addSubview(hosting.view)
view.contentSize = CGSize(width: width, height: size.height)
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<ContentView.MyScrollView>) {
}
}
@mishimay
Copy link
Author

I wrote an improved code.
Please check this.
https://gist.github.com/mishimay/b823280bd91d16474362376029321752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment