Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MainasuK/9584882163161d3a77d7236929251987 to your computer and use it in GitHub Desktop.
Save MainasuK/9584882163161d3a77d7236929251987 to your computer and use it in GitHub Desktop.
Generic structures to host previews of UIView and UIViewController subclasses. Also NSView and NSViewController
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct <#NSView#>_Preview: PreviewProvider {
static var previews: some View {
NSViewPreview {
<#Code#>
}
}
}
#endif
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct <#NSViewController#>_Preview: PreviewProvider {
static var previews: some View {
NSViewControllerPreview {
<#Code#>
}
}
}
#endif
import Cocoa
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct NSViewControllerPreview<ViewController: NSViewController>: NSViewControllerRepresentable {
let viewController: ViewController
init(_ builder: @escaping () -> ViewController) {
viewController = builder()
}
// MARK: - NSViewControllerRepresentable
func makeNSViewController(context: Context) -> ViewController {
return viewController
}
func updateNSViewController(_ viewController: ViewController, context: Context) {
}
}
#endif
import Cocoa
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct NSViewPreview<View: NSView>: NSViewRepresentable {
let view: View
init(_ builder: @escaping () -> View) {
view = builder()
}
// MARK: - NSViewRepresentable
func makeNSView(context: Context) -> View {
return view
}
func updateNSView(_ view: View, context: Context) {
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
}
}
#endif
#if DEBUG
import SwiftUI
struct <#UIView#>_Preview: PreviewProvider {
static var previews: some View {
UIViewPreview {
<#Code#>
}
}
}
#endif
#if DEBUG
import SwiftUI
struct <#UIViewController#>_Preview: PreviewProvider {
static var previews: some View {
UIViewControllerPreview {
<#Code#>
}
}
}
#endif
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 {
return viewController
}
func updateUIViewController(_ viewController: ViewController, context: Context) {
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment