Created
July 5, 2019 03:25
-
-
Save timothycosta/0d8f64afeca0b6cc29665d87de0d94d2 to your computer and use it in GitHub Desktop.
UIScrollView wrapped for SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIScrollViewWrapper.swift | |
// lingq-5 | |
// | |
// Created by Timothy Costa on 2019/07/05. | |
// Copyright © 2019 timothycosta.com. All rights reserved. | |
// | |
import SwiftUI | |
struct UIScrollViewWrapper<Content: View>: UIViewControllerRepresentable { | |
var content: () -> Content | |
init(@ViewBuilder content: @escaping () -> Content) { | |
self.content = content | |
} | |
func makeUIViewController(context: Context) -> UIScrollViewViewController { | |
let vc = UIScrollViewViewController() | |
vc.hostingController.rootView = AnyView(self.content()) | |
return vc | |
} | |
func updateUIViewController(_ viewController: UIScrollViewViewController, context: Context) { | |
viewController.hostingController.rootView = AnyView(self.content()) | |
} | |
} | |
class UIScrollViewViewController: UIViewController { | |
lazy var scrollView: UIScrollView = { | |
let v = UIScrollView() | |
v.isPagingEnabled = true | |
return v | |
}() | |
var hostingController: UIHostingController<AnyView> = UIHostingController(rootView: AnyView(EmptyView())) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.addSubview(self.scrollView) | |
self.pinEdges(of: self.scrollView, to: self.view) | |
self.hostingController.willMove(toParent: self) | |
self.scrollView.addSubview(self.hostingController.view) | |
self.pinEdges(of: self.hostingController.view, to: self.scrollView) | |
self.hostingController.didMove(toParent: self) | |
} | |
func pinEdges(of viewA: UIView, to viewB: UIView) { | |
viewA.translatesAutoresizingMaskIntoConstraints = false | |
viewB.addConstraints([ | |
viewA.leadingAnchor.constraint(equalTo: viewB.leadingAnchor), | |
viewA.trailingAnchor.constraint(equalTo: viewB.trailingAnchor), | |
viewA.topAnchor.constraint(equalTo: viewB.topAnchor), | |
viewA.bottomAnchor.constraint(equalTo: viewB.bottomAnchor), | |
]) | |
} | |
} | |
I'm having the same issue with the content height. Anybody get it working?
@wilg Did you ever get this working?
no, sorry
@mysterytoy @wilg I solved the issue on a similar component of mine by adding hostingController.sizingOptions = .intrinsicContentSize
in the UIScrollViewViewController
initializer.
Edit: Here is an excellent article about it: https://medium.com/@batrakov.vitaly/adapting-uihostingcontroller-to-changes-in-swiftui-view-size-da11a0994a1e
one more thing layoutIfNeeded must be called before returing the view because otherwise the view may freeze when updating the content in the scrollView
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great, just that if I put VStack in, VStack width is bound to its content and not greedy.