SwiftUIWrapper

πŸ‘€ ν•΄κ²°ν•˜κ³ μž ν•˜λŠ” 문제

κΈ°μ‘΄ UIKit ν”„λ‘œμ νŠΈμ—μ„œ SwiftUI λ₯Ό 자유둭게 μ‚¬μš©ν•˜κΈ° μœ„ν•΄ κ³ λ―Όν•˜λ‹€ λ§Œλ“€κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
UIHostingController λ₯Ό 톡해 SwiftUI View λ₯Ό ν˜ΈμΆœν•˜κ²Œ 될 경우 μ˜ˆμƒν•˜μ§€ λͺ»ν•œ λ§Žμ€ 버그λ₯Ό λ§ˆμ£Όν•˜κ³€ ν•©λ‹ˆλ‹€.
κ·ΈλŸ¬ν•œ 이슈λ₯Ό 미리 μ˜ˆλ°©ν•  수 μžˆλŠ” 방법을 κ³ λ―Όν•˜λ‹€ κ³ μ•ˆν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

SwiftUI, UIKit λ₯Ό 같이 μ‚¬μš©ν•˜λ‹€ λ³΄λ‹ˆ ν†΅μΌλ˜μ§€ λͺ»ν•œ ν™”λ©΄ μ „ν™˜ 방식에 λŒ€ν•΄ 고민을 ν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ NavigationController λ₯Ό μΊ‘μŠν™”ν•˜μ—¬ UIKit, SwiftUI λͺ¨λ‘μ—μ„œ ν†΅μΌλœ λ°©λ²•μœΌλ‘œ ν™”λ©΄ μ „ν™˜ ν•  수 μžˆλ„λ‘ ꡬ쑰화 ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
λ˜ν•œ κΈ°μ‘΄ SwiftUI 처럼 View μ—μ„œ 직접 Navigation 을 ν•˜μ§€ μ•Šκ³  navigator λΌλŠ” 객체λ₯Ό 톡해 ViewModel ν˜Ήμ€ μ™ΈλΆ€μ—μ„œ 화면을 μ „ν™˜ν•  수 μžˆλ‹€λŠ” μž₯점도 μžˆμŠ΅λ‹ˆλ‹€.

πŸ› οΈ μ‚¬μš©λœ λ””μžμΈ νŒ¨ν„΄

  • Adapter Pattern
  • Command Pattern

πŸ‘ μž₯점

  1. NavigationController λ₯Ό SwiftUI μ—μ„œ 도 UIKit 와 μΌκ΄€λœ λ°©λ²•μœΌλ‘œ ν™”λ©΄ μ „ν™˜ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  2. κΈ°μ‘΄ SwiftUI 처럼 View μ—μ„œ 직접 navigate ν•˜μ§€ μ•Šκ³  navigator λΌλŠ” 객체λ₯Ό 톡해 viewModel λ“±μ—μ„œ navigation 을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  3. UIHostingControllerλ₯Ό μ‚¬μš©ν•΄μ„œ λ°œμƒν•˜λŠ” 버그듀을 μ˜ˆλ°©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ“± μ½”λ“œ μ˜ˆμ‹œμ™€ ν™”λ©΄

βœ”οΈ SwiftUI Side

struct RootView: NavigatableView {
    
    weak var navigator: VCNavigator?
    
    var body: some View {
       ...
    }
    
    func didTapButton() {
    
        let destination = StackView
            .init()
            .wrap()
            
        navigator?.push(to: destination)
    }
    
    func didTapDismiss() {
        navigator?.pop()
    }
}

βœ”οΈ UIKit Side

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

    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene
    
    let controller = RootView
        .init()
        .wrap()
        
    let nController = UINavigationController(rootViewController: controller)
    
    window?.rootViewController = nController
    window?.makeKeyAndVisible()
}

πŸ“š μ°Έκ³  (μ„œμ )