実装
SwiftUIViewController.swift
というファイルを追加
import UIKit
import SwiftUI
struct SwiftUIViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController {
let storyboard = UIStoryboard(name: "<#Name#>", bundle: nil)
guard
let viewController = storyboard.instantiateInitialViewController() as? ViewController
else {
fatalError("Cannot load ViewController from Main storyboard.")
}
return viewController
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {}
}
// Xibの場合はこっち
// struct SwiftUIViewController: UIViewControllerRepresentable {
// func makeUIViewController(context: Context) -> UIViewController {
// let viewController = UIViewController(nibName: "<#Name#>", bundle: nil)
// return viewController
// }
//
// func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
// }
ViewController
側にこれを追記
struct ViewControllerPreview: PreviewProvider {
static var previews: some View {
SwiftUIViewController()
}
}
これでPreviewが使えるようになります。