ios - SwiftUI View 和 UIKit View 的 z-index

标签 ios swift uikit swiftui z-index

SwiftUI ScrollView 隐藏了 UIViewControllerRepresentable viewController 呈现的 View 的某些区域。

enter image description here

作为 SwiftUI 代码的一部分,GoogleMapsView 是 UIViewControllerRepresentable viewController。

struct ContentView: View {

var body: some View {
    ZStack(alignment: .top) {

        GoogleMapsView()

        VStack(alignment: .leading) {
            Text("Top Locations near you")

            ScrollView(.horizontal, showsIndicators: false) {
                HStack() {
                    ForEach(dataSource.topPlaces) { place in
                        PlaceCardView(placeImage: place.image, placeName: place.name, placeRating: place.rating, placeRatingTotal: place.ratingTotal, topPlace: place)
                    }
                }
                .padding(.horizontal, 10)
            }
            .background(Color.clear)
            .foregroundColor(.black)
            .frame(height: 200)
            .opacity(self.finishedFetching ? 1 : 0.1)
            .animation(.easeInOut(duration: 1.3))           
    }.edgesIgnoringSafeArea(.all)
}

我想要放在顶部的 View 来自 GoogleMapView(),我将它放在 ZStack 的“底部”,因为我希望 ScrollView 在 map 上流动。然而,点击标记时 map 上显示的 View 被 SwiftUI ScrollView 覆盖

我尝试更改 ScrollView 的 zIndex、弹出 View 的 zPosition 或 BringSubviewToFront 等。但它们都不起作用。

最佳答案

您需要通过绑定(bind)将本地状态注入(inject)``并在显示/隐藏的弹出窗口中更改它。

这是一些伪代码

struct ContentView: View {

@State private var isPopup = false

var body: some View {
    ZStack(alignment: .top) {

        GoogleMapsView(isPopup: $isPopup)

        VStack(alignment: .leading) {
            Text("Top Locations near you")

            if !isPopup {
               ScrollView(.horizontal, showsIndicators: false) {
                  // other code
               }
               // other code
            }
    }.edgesIgnoringSafeArea(.all)
}

struct GoogleMapsView : UIViewControllerRepresentable {
   @Binding isPopup: Bool

   // other code

   // make self.isPopup = true before popup and false after

关于ios - SwiftUI View 和 UIKit View 的 z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62157175/

相关文章:

javascript - 如何在 WKWebView 中检测 history.pushstate

ios - UIView.animateWithDuration 不是动画 Swift(再次)

c# - 从 IntPtr 创建 NSException

javascript - iOS Safari 无法通过 AJAX 发送表单数据

ios - 找不到您输入的 apple id 或您的密码不正确。请再次尝试测试应用内购买

ios - 如何从 iOS 中的 Healthkit 实时获取心率数据?

ios - 工具栏栏按钮项没有 IBAction 选项

swift - 进行可选链的好方法

iphone - UITableView insertSections : withRowAnimation: causing all table cells to be requested

iphone - 如何动态调整 UITableView 高度?