From d956afc57ded6cfc99295ec3a9f198017f69ae15 Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Wed, 14 Feb 2024 13:49:49 +0400 Subject: [PATCH] SwiftUI in Compose with ComposeUIViewController() (#4297) Update sample project about "How to use SwiftUI in Compose" --- .../ios-swiftui-in-compose/gradle.properties | 4 +-- .../ComposeViewControllerRepresentable.swift | 31 +++---------------- .../shared/src/iosMain/kotlin/main.ios.kt | 9 ++++-- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/examples/interop/ios-swiftui-in-compose/gradle.properties b/examples/interop/ios-swiftui-in-compose/gradle.properties index 9709e7baad..9197577af3 100644 --- a/examples/interop/ios-swiftui-in-compose/gradle.properties +++ b/examples/interop/ios-swiftui-in-compose/gradle.properties @@ -3,5 +3,5 @@ xcodeproj=./iosApp org.gradle.jvmargs=-Xmx3g # Enable kotlin/native experimental memory model kotlin.native.binary.memoryModel=experimental -kotlin.version=1.9.21 -compose.version=1.5.11 +kotlin.version=1.9.22 +compose.version=1.6.0-rc01 diff --git a/examples/interop/ios-swiftui-in-compose/iosApp/iosApp/ComposeViewControllerRepresentable.swift b/examples/interop/ios-swiftui-in-compose/iosApp/iosApp/ComposeViewControllerRepresentable.swift index 05ec00cba7..0c02a6a54e 100644 --- a/examples/interop/ios-swiftui-in-compose/iosApp/iosApp/ComposeViewControllerRepresentable.swift +++ b/examples/interop/ios-swiftui-in-compose/iosApp/iosApp/ComposeViewControllerRepresentable.swift @@ -5,35 +5,14 @@ import MapKit struct ComposeViewControllerRepresentable: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> UIViewController { - return Main_iosKt.ComposeEntryPointWithUIView(createUIView: { () -> UIView in - SwiftUIInUIView( - content: VStack { - Text("SwiftUI in Compose Multiplatform") - } - ) + return Main_iosKt.ComposeEntryPointWithUIViewController(createUIViewController: { () -> UIViewController in + let swiftUIView = VStack { + Text("SwiftUI in Compose Multiplatform") + } + return UIHostingController(rootView: swiftUIView) }) } func updateUIViewController(_ uiViewController: UIViewController, context: Context) { } } - -private class SwiftUIInUIView: UIView { - - init(content: Content) { - super.init(frame: CGRect()) - let hostingController = UIHostingController(rootView: content) - hostingController.view.translatesAutoresizingMaskIntoConstraints = false - addSubview(hostingController.view) - NSLayoutConstraint.activate([ - hostingController.view.topAnchor.constraint(equalTo: topAnchor), - hostingController.view.leadingAnchor.constraint(equalTo: leadingAnchor), - hostingController.view.trailingAnchor.constraint(equalTo: trailingAnchor), - hostingController.view.bottomAnchor.constraint(equalTo: bottomAnchor) - ]) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } -} diff --git a/examples/interop/ios-swiftui-in-compose/shared/src/iosMain/kotlin/main.ios.kt b/examples/interop/ios-swiftui-in-compose/shared/src/iosMain/kotlin/main.ios.kt index 7486a6ec34..7af0d60047 100644 --- a/examples/interop/ios-swiftui-in-compose/shared/src/iosMain/kotlin/main.ios.kt +++ b/examples/interop/ios-swiftui-in-compose/shared/src/iosMain/kotlin/main.ios.kt @@ -5,6 +5,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.interop.UIKitView +import androidx.compose.ui.interop.UIKitViewController import androidx.compose.ui.unit.dp import androidx.compose.ui.window.ComposeUIViewController import kotlinx.cinterop.ExperimentalForeignApi @@ -12,7 +13,9 @@ import platform.UIKit.UIView import platform.UIKit.UIViewController @OptIn(ExperimentalForeignApi::class) -fun ComposeEntryPointWithUIView(createUIView: () -> UIView): UIViewController = +fun ComposeEntryPointWithUIViewController( + createUIViewController: () -> UIViewController +): UIViewController = ComposeUIViewController { Column( Modifier @@ -21,8 +24,8 @@ fun ComposeEntryPointWithUIView(createUIView: () -> UIView): UIViewController = horizontalAlignment = Alignment.CenterHorizontally ) { Text("How to use SwiftUI inside Compose Multiplatform") - UIKitView( - factory = createUIView, + UIKitViewController( + factory = createUIViewController, modifier = Modifier.size(300.dp).border(2.dp, Color.Blue), ) }