Storyboard 없이 UI 화면 작성 시 초기 설정하기

1. Canvas & UIViewControllerRepresentable 사용하여 Preview를 볼 수 있는 코드(필수 아님)

  • UIViewControllerRepresntable: UIkit로 만들어진 UIView를 SwiftUI에서 사용하기 위한 프로토콜

      //아래 코드를 canvas를 사용하고 싶은 ViewController 파일 내에 추가
      #if canImport(SwiftUI) && DEBUG
      import SwiftUI
    
      @available(iOS 13.0.0, *)
      struct TestViewControllerRepresentable: UIViewControllerRepresentable {
            
          typealias UIViewControllerType = ViewController
            
          func makeUIViewController(context: Context) -> ViewController {
              return ViewController()
          }
            
          func updateUIViewController(_ uiViewController: ViewController, context: Context) {
                
          }
            
      }
    
      struct ViewPreview: PreviewProvider {
          static var previews: some View{
              TestViewControllerRepresentable()
          }
      }
    
      #endif
    
    

2. 자동 생성 된 Storyboard 삭제하기

  1. Main.storyboard delete
  2. info.plist에서 Application SceneManifest > Scene Configuration > Application Session Role > Item 0 > Stroryboard Name field 삭제
  3. project.xcodeproj에서 Deployment Info > Main Interface 값 삭제
  4. SceneDelegate.swift에 다음 코드 작성

     ```swift
     guard let windowScene = (scene as? UIWindowScene) else { return }
             window = UIWindow(windowScene: windowScene) // SceneDelegate의 프로퍼티에 설정해줌
             let mainViewController = ViewController() // 맨 처음 보여줄 ViewController
    
             window?.rootViewController = mainViewController
             window?.makeKeyAndVisible()
     ```
    
  5. ViewController에 배경색 지정(default = black)

     ```swift
     //ViewController.swift
     view.backgroundColor = .white
     ```