Skip to content

Instantly share code, notes, and snippets.

@sergeyshalnov
Last active April 4, 2025 11:17
Show Gist options
  • Save sergeyshalnov/4c150c4f97cb898a04f826b041c32b13 to your computer and use it in GitHub Desktop.
Save sergeyshalnov/4c150c4f97cb898a04f826b041c32b13 to your computer and use it in GitHub Desktop.
UIView preview snippet
#if DEBUG
import UIKit
import SwiftUI
// MARK: - Preview Struct
@available(iOS 13, *)
extension UIView {
private struct Preview<V: UIView>: UIViewRepresentable {
// MARK: - Properties
let view: V
// MARK: - Methods
func makeUIView(context: UIViewRepresentableContext<Preview<V>>) -> V {
return view
}
func updateUIView(_ uiView: V, context: UIViewRepresentableContext<Preview<V>>) { }
}
}
// MARK: - Preview Property
@available(iOS 13, *)
extension UIView {
var preview: some View {
Preview(view: self)
}
}
#endif
#if DEBUG
import SwiftUI
@available(iOS 13, *)
struct Preview: PreviewProvider {
static var previews: some View {
return UIView().preview
.edgesIgnoringSafeArea(.all)
.previewLayout(.fixed(width: 320, height: 400))
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment