Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active October 7, 2024 06:51
Show Gist options
  • Save mattt/ff6b58af8576c798485b449269d43607 to your computer and use it in GitHub Desktop.
Save mattt/ff6b58af8576c798485b449269d43607 to your computer and use it in GitHub Desktop.
Generic structures to host previews of UIView and UIViewController subclasses.
import UIKit
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable {
let viewController: ViewController
init(_ builder: @escaping () -> ViewController) {
viewController = builder()
}
// MARK: - UIViewControllerRepresentable
func makeUIViewController(context: Context) -> ViewController {
viewController
}
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<UIViewControllerPreview<ViewController>>) {
return
}
}
#endif
import UIKit
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewPreview<View: UIView>: UIViewRepresentable {
let view: View
init(_ builder: @escaping () -> View) {
view = builder()
}
// MARK: - UIViewRepresentable
func makeUIView(context: Context) -> UIView {
return view
}
func updateUIView(_ view: UIView, context: Context) {
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
}
}
#endif
@mattt
Copy link
Author

mattt commented Feb 7, 2020

@bielikb Cool, thanks for sharing. Only real correction I'd offer is that my name is Mattt, not Matt Thompson.

@bielikb
Copy link

bielikb commented Feb 7, 2020

Thanks, ill fix it right after the lunch 🙂

EDIT:
done

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