All Articles

UIKitで作られたViewControllerのPreview

happy halloween

ソース

class MainController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    view.background = .red
  }
}

import SwiftUI
struct MainPreview: PreviewProvider {
   static var previews: some View {
       ContainerView().edgesIgnoringSafeArea(.all)
   }

   struct ContainerView: UIViewControllerRrepresentable {
      func makeUIViewController(context: UIViewControllerRepresentableContext<MainPreview.ContainerView> -> UIViewController {
           return MainController()
      }
   }
}

UIViewControllerRrepresentable の makeUIViewController の中に UIKit の ViewController を追加する