Last active
November 15, 2022 14:05
-
-
Save buh/253d969f6722ce171bbfd9c272910b24 to your computer and use it in GitHub Desktop.
Helpers to quickly create SwiftUI previews for the UIKit/AppKit view and view controller
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 SwiftUI | |
#if canImport(UIKit) | |
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .systemYellow | |
title = "Hello World!" | |
} | |
} | |
@available(iOS 14.0, *) // This is only needed for .ignoresSafeArea() | |
struct ViewController_Previews: PreviewProvider { | |
static var previews: some View { | |
UIViewControllerRepresentableProvider { _ in | |
let nav = UINavigationController(rootViewController: ViewController()) | |
nav.navigationBar.prefersLargeTitles = true | |
return nav | |
} | |
.ignoresSafeArea() | |
// For a UIView. | |
UIViewRepresentableProvider { _ in | |
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "") | |
cell.textLabel?.text = "Hello" | |
cell.detailTextLabel?.text = "World!" | |
return cell | |
} | |
} | |
} | |
#endif |
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
/// | |
/// Helpers to quickly create SwiftUI previews for the UIKit/AppKit view and view controller. | |
/// | |
/// For example (UIKit): | |
/// ``` | |
/// @available(iOS 14.0, *) // This is only needed for .ignoresSafeArea() | |
/// struct YourViewController_Previews: PreviewProvider { | |
/// static var previews: some View { | |
/// UIViewControllerRepresentableProvider { _ in | |
/// YourViewController() | |
/// } | |
/// .ignoresSafeArea() | |
/// | |
/// // For a UIView. | |
/// UIViewRepresentableProvider { _ in | |
/// let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "") | |
/// cell.textLabel?.text = "Hello" | |
/// cell.detailTextLabel?.text = "World!" | |
/// return cell | |
/// } | |
/// } | |
/// } | |
/// ``` | |
/// | |
import SwiftUI | |
#if canImport(UIKit) && DEBUG | |
import UIKit | |
struct UIViewControllerRepresentableProvider<T: UIViewController>: UIViewControllerRepresentable { | |
/// Creates an instance of ``UIViewController`` for previewing. | |
let makingUIViewController: (_ context: Context) -> T | |
func makeUIViewController(context: Context) -> T { | |
makingUIViewController(context) | |
} | |
func updateUIViewController(_ uiViewController: T, context: Context) {} | |
} | |
struct UIViewRepresentableProvider<T: UIView>: UIViewRepresentable { | |
/// Creates an instance of ``UIView`` for previewing. | |
let makingUIView: (_ context: Context) -> T | |
func makeUIView(context: Context) -> T { | |
makingUIView(context) | |
} | |
func updateUIView(_ uiView: T, context: Context) {} | |
} | |
#endif | |
#if canImport(AppKit) && DEBUG | |
import AppKit | |
struct NSViewControllerRepresentableProvider<T: NSViewController>: NSViewControllerRepresentable { | |
/// Creates an instance of ``NSViewController`` for previewing. | |
let makingNSViewController: (_ context: Context) -> T | |
func makeNSViewController(context: Context) -> T { | |
makingNSViewController(context) | |
} | |
func updateNSViewController(_ nsViewController: T, context: Context) {} | |
} | |
struct NSViewRepresentableProvider<T: NSView>: NSViewRepresentable { | |
/// Creates an instance of ``NSView`` for previewing. | |
let makingNSView: (_ context: Context) -> T | |
func makeNSView(context: Context) -> T { | |
makingNSView(context) | |
} | |
func updateNSView(_ nsView: T, context: Context) {} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment