I'm unable to render a Compose Multiplatform view inside a SwiftUI Form
. However, it works outside of the Form
.
SwiftUI view:
struct ContentView: View {
var body: some View {
Form {
Section {
KMPView()
}
}
KMPView()
}
}
struct KMPView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return KMPView_iosKt.ComposeEntryPoint()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
KMPView.ios.kt:
import androidxpose.ui.window.ComposeUIViewController
import androidxpose.material.Text
import platform.UIKit.UIViewController
fun ComposeEntryPoint(): UIViewController =
ComposeUIViewController {
Text("Hello from Compose")
}
This results in the view inside the Form
not being rendered but the one outside of it works fine
I'm unable to render a Compose Multiplatform view inside a SwiftUI Form
. However, it works outside of the Form
.
SwiftUI view:
struct ContentView: View {
var body: some View {
Form {
Section {
KMPView()
}
}
KMPView()
}
}
struct KMPView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return KMPView_iosKt.ComposeEntryPoint()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
KMPView.ios.kt:
import androidxpose.ui.window.ComposeUIViewController
import androidxpose.material.Text
import platform.UIKit.UIViewController
fun ComposeEntryPoint(): UIViewController =
ComposeUIViewController {
Text("Hello from Compose")
}
This results in the view inside the Form
not being rendered but the one outside of it works fine
1 Answer
Reset to default 0Try adding a size. It likely has to do with default constrainsts.
KMPView()
.frame(width: 200, height: 200)
//or
.frame(maxWidth: .infinity, maxHeight: .infinity)
Form is a type of scroll view and scroll views default views to zero when they can’t find an ideal size.
It can natively happen in SwiftUI if you put a ScrollView
inside a List
. The inner view would have no default size.