このはじめに
SwiftUIからViewControllerを表示する時のTipsです。
ViewControllerとStoryboard
記事用に作成したViewControllerとStoryboardになります。
ViewController
SampleViewController.swift
import UIKit
import Instantiate
import InstantiateStandard
class SampleViewController: UIViewController, StoryboardInstantiatable {
override func viewDidLoad() {
super.viewDidLoad()
}
}
Storyboard
UIViewControllerRepresentableを使用
SampleViewController.swift
import UIKit
import SwiftUI
import Instantiate
import InstantiateStandard
class SampleViewController: UIViewController, StoryboardInstantiatable {
override func viewDidLoad() {
super.viewDidLoad()
}
}
struct SampleViewControllerWrapper : UIViewControllerRepresentable {
typealias UIViewControllerType = SampleViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<SampleViewControllerWrapper>) -> SampleViewControllerWrapper.UIViewControllerType {
return UIViewControllerType.instantiate()
}
func updateUIViewController(_ uiViewController: SampleViewControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<SampleViewControllerWrapper>) {
}
}
SwiftUIからSampleViewControllerを表示する
SampleView.swift
import SwiftUI
struct SampleView: View {
var body: some View {
SampleViewControllerWrapper()
}
}
#if DEBUG
struct SampleView_Previews: PreviewProvider {
static var previews: some View {
SampleView()
}
}
#endif