Skip to content

Instantly share code, notes, and snippets.

@SergLam
Created April 25, 2022 18:20
Show Gist options
  • Save SergLam/a36a21ef9a643bea05157557f07f26f1 to your computer and use it in GitHub Desktop.
Save SergLam/a36a21ef9a643bea05157557f07f26f1 to your computer and use it in GitHub Desktop.
SwiftUI Previews for UIKit
import UIKit
final class SampleTableViewCell: UITableViewCell {
static let cellHeight: CGFloat = 60.0
static let reuseIdentifier: String = String(describing: SampleTableViewCell.self)
private let nameLabel: UILabel = UILabel()
func update(with model: String) {
nameLabel.text = model
}
}
#if DEBUG
#if targetEnvironment(simulator)
import SwiftUI
@available(iOS 13.0, *)
struct SampleTableViewCell_Previews: PreviewProvider {
static var devices = ["iPhone 13 Pro Max"]
static var platform: PreviewPlatform? {
return SwiftUI.PreviewPlatform.iOS
}
static var previews: some SwiftUI.View {
let height: CGFloat = SampleTableViewCell.cellHeight
ForEach(devices, id: \.self) { deviceName in
VStack(alignment: .center, spacing: 10) {
UIViewPreview {
let view = SampleTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: SampleTableViewCell.reuseIdentifier)
view.update(with: "Test")
return view
}
.frame(minWidth: 70, maxWidth: .infinity, minHeight: height, maxHeight: height, alignment: .center)
Spacer()
}
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
#endif
#endif
import UIKit
final class SampleView: UIView {
private let nameLabel: UILabel = UILabel()
func update(with model: String) {
nameLabel.text = model
}
}
#if DEBUG
#if targetEnvironment(simulator)
import SwiftUI
@available(iOS 13.0, *)
struct SampleView_Previews: PreviewProvider {
static var devices = ["iPhone 13 Pro Max"]
static var platform: PreviewPlatform? {
return SwiftUI.PreviewPlatform.iOS
}
static var previews: some SwiftUI.View {
ForEach(devices, id: \.self) { deviceName in
VStack(alignment: .center, spacing: 10) {
UIViewPreview {
let view = SampleView(frame: CGRect.zero)
view.update(with: "Test")
return view
}
.frame(minWidth: 70, maxWidth: .infinity, minHeight: 0.0, maxHeight: .infinity, alignment: .center)
}
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
#endif
#endif
import UIKit
final class SampleViewController: UIViewController {
}
#if DEBUG
#if targetEnvironment(simulator)
import SwiftUI
@available(iOS 13.0, *)
struct SampleViewController_Previews: PreviewProvider {
static var devices = ["iPhone 13 Pro Max"]
static var platform: PreviewPlatform? {
return SwiftUI.PreviewPlatform.iOS
}
static var previews: some SwiftUI.View {
ForEach(devices, id: \.self) { deviceName in
Group {
UIViewControllerPreview {
let vc = SampleViewController()
return vc
}
.frame(minWidth: 70, maxWidth: .infinity, minHeight: 0.0, maxHeight: .infinity, alignment: .center)
}
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
#endif
#endif
import SwiftUI
import UIKit
@available(iOS 13.0, *)
struct UIViewControllerPreview: SwiftUI.View {
private let factory: () -> UIViewController
init(factory: @escaping () -> UIViewController) {
self.factory = factory
}
var body: some SwiftUI.View {
Renderer(factory)
}
private struct Renderer: UIViewControllerRepresentable {
private let factory: () -> UIViewController
init(_ factory: @escaping () -> UIViewController) {
self.factory = factory
}
func makeUIViewController(context: Context) -> UIViewController {
return factory()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
}
import SwiftUI
import UIKit
@available(iOS 13.0, *)
struct UIViewPreview: SwiftUI.View {
private let factory: () -> UIView
init(factory: @escaping () -> UIView) {
self.factory = factory
}
var body: some SwiftUI.View {
Renderer(factory)
}
private struct Renderer: UIViewRepresentable {
private let factory: () -> UIView
init(_ factory: @escaping () -> UIView) {
self.factory = factory
}
func makeUIView(context: Context) -> UIView {
return factory()
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment