Browse Source

SwiftUI in Compose with ComposeUIViewController() (#4297)

Update sample project about "How to use SwiftUI in Compose"
pull/4382/head
dima.avdeev 3 months ago committed by GitHub
parent
commit
d956afc57d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      examples/interop/ios-swiftui-in-compose/gradle.properties
  2. 31
      examples/interop/ios-swiftui-in-compose/iosApp/iosApp/ComposeViewControllerRepresentable.swift
  3. 9
      examples/interop/ios-swiftui-in-compose/shared/src/iosMain/kotlin/main.ios.kt

4
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

31
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<Content: View>: 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")
}
}

9
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),
)
}

Loading…
Cancel
Save