SwiftUI ScrollView 隐藏了 UIViewControllerRepresentable viewController 呈现的 View 的某些区域。
作为 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/