SwiftUI 100 레시피/UIKit and SwiftUI Interoperability

SwiftUI UIKit 71 - UIKit앱에 SwiftUI 적용하기

tonylee9239 2022. 6. 28. 15:50

#1. Storyboard로 프로젝트 생성

#2. Info.pilist에서 Main 삭제

 

#3. Storyboard Name key 삭제

삭제전
삭제후

 

#4. RatingView 끌어와서 폴더에 삽입

RatingView swfitUI 화일 끌어와서 삽입

 

#5. MovieListScreen SwiftUI화일 생성

 

import SwiftUI

 

struct MovieListScreen: View {

    var body: some View {

        List(1...20, id: \.self){ index in

            HStack {

                Text("Movie \(index)")

                Spacer()

                RatingView(rating: .constant(Int.random(in: 1...5)))

            }

        }

    }

}

 

struct MovieListScreen_Previews: PreviewProvider {

    static var previews: some View {

        MovieListScreen()

    }

}

 

#6. SceneDelegate 수정

 

import UIKit

import SwiftUI  //추가

 

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

 

    var window: UIWindow?

 

 

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.

        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.

        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

 

 

    //수정전    

    //guard let _ = (scene as? UIWindowScene) else { return } 

      

     // 수정후

      guard let windowScene = (scene as? UIWindowScene) else { return }

        

        let window = UIWindow(windowScene: windowScene)

        window.rootViewController = UIHostingController(rootView: MovieListScreen())

        window.makeKeyAndVisible()

        self.window = window

    }