UIKit

UIKit | SwiftUI Preview 적용하기 (iOS 15.ver)

ziziDev 2024. 8. 13. 12:25
반응형
import UIKit
import SwiftUI

class AlarmViewController: UIViewController {
 private var alarmView = AlarmView(frame: .zero)
 override func loadView() {
  alarmView = AlarmView(frame: UIScreen.main.bounds)
  self.view = alarmView
 }
 override func viewDidLoad() {
  super.viewDidLoad()
  view.backgroundColor = .green
 }
}
//여기부터 보면됨
struct PreView: PreviewProvider {
 static var previews: some View {
  AlarmViewController().toPreview()
 }
}
#if DEBUG
extension UIViewController {
 private struct Preview: UIViewControllerRepresentable {
  let viewController: UIViewController
  func makeUIViewController(context: Context) -> UIViewController {
  return viewController
  }
  func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
  }
 }
 func toPreview() -> some View {
  Preview(viewController: self)
 }
}
#endif

 

만약 화면이 아예 생성이 안된다고 할 때 

 

option + command + Enter

 

클릭하시면 볼 수 있습니다

 

 

짜잔

반응형