SwiftUI에서 쓰이는
PreviewProvider를 UIKit에서 사용해보자.
코드를 먼저 보면,
import UIKit
import SwiftUI
class MainViewController:UIViewContoller {
...
}
//PrewViewProvider 연결!
struct MainViewController_Previews: PreviewProvider {
//프리뷰
static var previews: some View {
container().edgesIgnoringSafeArea(.all)
}
//프리뷰 보여줄 ViewController 지정
struct container: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
let mainViewController = MainViewController()
return UINavigationController(rootViewController: mainViewController)
}
//호출해야하는 함수라 비워두어도 괜찮다능
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
//타입알리아스는 UIViewController로
typealias UIViewControllerType = UIViewController
}
}
먼저 Swift UI를 임포트 해준다.
버전체크는 디버깅하면서 해둔것인데 단순히 사용만 하는거라면 꼭 필요하진 않다.
그런데
Preview가 안뜬다면 무엇무엇을 확인해봐야할까?!