Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Created February 13, 2024 22:12
Show Gist options
  • Save dnedrow/e6b6e0f2e45de3ae4d340be65fe96ccf to your computer and use it in GitHub Desktop.
Save dnedrow/e6b6e0f2e45de3ae4d340be65fe96ccf to your computer and use it in GitHub Desktop.
Wrapper to allow UIViews to preview in SwiftUI preview pane
import Foundation
import SwiftUI
import UIKit
///
/// Wraps a UIView such that it can be shown in the SwifUI
/// preview pane.
/// Usage:
/// ```
/// struct SampleView_Previews: PreviewProvider {
/// static var previews: some View {
/// PreviewContainer {
/// return SampleView()
/// }
/// }
/// }
/// ```
struct PreviewContainer<T: UIView>: UIViewRepresentable {
let view: T
init(_ viewBuilder: @escaping () -> T) {
view = viewBuilder()
}
// MARK: - UIViewRepresentable
func makeUIView(context: Context) -> T {
return view
}
func updateUIView(_ view: T, context: Context) {
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment