Created
February 13, 2024 22:12
-
-
Save dnedrow/e6b6e0f2e45de3ae4d340be65fe96ccf to your computer and use it in GitHub Desktop.
Wrapper to allow UIViews to preview in SwiftUI preview pane
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
| 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