0

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 androidx.compose.ui.window.ComposeUIViewController
import androidx.compose.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 1

0

Try 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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried both of those, unfortunately neither solves the problem. The second one results in the same as the original. The first one creates a bigger blank box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.