如何在 iOS 上的 Compose Multiplatform 视图上设置透明背景?

问题描述 投票:0回答:1

我在 Kotlin 中使用 org.jetbrains.compose v 1.4.0 进行了最简单的设置

fun MainViewController() =
    ComposeUIViewController {
       Button(onClick = { }) {
            Text("Click me")
        }
    }

在 iOS 上我有:

struct ComposeView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        MainIosKt.MainViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

struct ContentView: View {
    @State var showSettings = false
    
    var body: some View {
        VStack {
            
            ZStack {
                Rectangle()
                    .fill(.red)
                    .frame(width: 500, height: 500)
                ComposeView()
                    .padding(EdgeInsets(top: 140, leading: 140, bottom: 140, trailing: 140))
                
            }
        }
    }
}

即使没有专门设置,也会有白色背景遮挡红色矩形。我想要透明背景,以便我可以将撰写 UI 覆盖在其他 iOS 内容(例如相机提要)上。

尝试:

  1. 更改撰写主题背景和表面
  2. 将 ComposeView modalPresentationStyle 更改为 .currentContext 或 .overCurrentContext
  3. 更改 ComposeView vc.view.backgroundColor = UIColor.clear

但是白色背景仍然存在。现在不支持还是我做错了什么?

swiftui transparency compose-multiplatform
1个回答
0
投票

感谢这个松弛的对话:https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1685005635951389

并更新至 compose-multiplatform 1.5.0 只需从对话中添加两个文件并像这样使用它:

    fun MainViewController() =
        TransparentComposeUIViewController {
           Button(onClick = { }) {
                Text("Click me")
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.