Skip to content

Instantly share code, notes, and snippets.

@yoxisem544
Created May 12, 2023 06:01
Show Gist options
  • Save yoxisem544/21b58a9f3bacfb983b7e94b463aa1f71 to your computer and use it in GitHub Desktop.
Save yoxisem544/21b58a9f3bacfb983b7e94b463aa1f71 to your computer and use it in GitHub Desktop.
#if canImport(swiftUI) && DEBUG
import UIKit
import SwiftUI
/// This is a UIKit preview in SwiftUI extension
/// With this extension, you will have a `.preview` property to get a swift ui preview view
@available(iOS 13.0, *)
extension UIViewController {
private struct Preview: UIViewControllerRepresentable {
let viewController: UIViewController
func makeUIViewController(context: Context) -> UIViewController {
viewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { /* no-op */ }
}
public var preview: some View {
return Preview(viewController: self)
}
}
#endif
// MARK: - Example code
// MARK: - View Controller
// #if canImport(swiftUI) && DEBUG
// import SwiftUI
// @available(iOS 13.0, *)
// struct <#ViewControllerYouWouldLikeToPreview#>_Preview: PreviewProvider {
// static var previews: some View {
// <#ViewControllerYouWouldLikeToPreview#>().preview
// }
// }
// #endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment