最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

interop - Compose Multiplatform composable not rendering inside SwiftUI Form - Stack Overflow

programmeradmin2浏览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 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

Share Improve this question asked Mar 18 at 20:10 Oskar PerssonOskar Persson 6,76517 gold badges69 silver badges133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论