κΈ°μ‘΄ UIKit νλ‘μ νΈμμ SwiftUI λ₯Ό μμ λ‘κ² μ¬μ©νκΈ° μν΄ κ³ λ―Όνλ€ λ§λ€κ² λμμ΅λλ€.
UIHostingController λ₯Ό ν΅ν΄ SwiftUI View λ₯Ό νΈμΆνκ² λ κ²½μ° μμνμ§ λͺ»ν λ§μ λ²κ·Έλ₯Ό λ§μ£Όνκ³€ ν©λλ€.
κ·Έλ¬ν μ΄μλ₯Ό 미리 μλ°©ν μ μλ λ°©λ²μ κ³ λ―Όνλ€ κ³ μνκ² λμμ΅λλ€.
SwiftUI, UIKit λ₯Ό κ°μ΄ μ¬μ©νλ€ λ³΄λ ν΅μΌλμ§ λͺ»ν νλ©΄ μ ν λ°©μμ λν΄ κ³ λ―Όμ νκ² λμμ΅λλ€.
κ·Έλμ NavigationController λ₯Ό μΊ‘μννμ¬ UIKit, SwiftUI λͺ¨λμμ ν΅μΌλ λ°©λ²μΌλ‘ νλ©΄ μ ν ν μ μλλ‘ κ΅¬μ‘°ν νμμ΅λλ€.
λν κΈ°μ‘΄ SwiftUI μ²λΌ View μμ μ§μ Navigation μ νμ§ μκ³ navigator λΌλ κ°μ²΄λ₯Ό ν΅ν΄ ViewModel νΉμ μΈλΆμμ νλ©΄μ μ νν μ μλ€λ μ₯μ λ μμ΅λλ€.
- Adapter Pattern
- Command Pattern
-
NavigationController λ₯Ό SwiftUI μμ λ UIKit μ μΌκ΄λ λ°©λ²μΌλ‘ νλ©΄ μ ν ν μ μμ΅λλ€.
-
κΈ°μ‘΄ SwiftUI μ²λΌ View μμ μ§μ navigate νμ§ μκ³ navigator λΌλ κ°μ²΄λ₯Ό ν΅ν΄ viewModel λ±μμ navigation μ ν μ μμ΅λλ€.
-
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()
}