【问题标题】:Why does ZStack only work in my ContentView?为什么 ZStack 只能在我的 ContentView 中工作?
【发布时间】:2020-08-08 06:06:53
【问题描述】:

我有一个 HomeView,它基本上是 MapView 和 Circle 的 ZStack,我正在关注 https://www.hackingwithswift.com/books/ios-swiftui/advanced-mkmapview-with-swiftui

当我在 ContentView 中使用 ZStack 时,地图和圆圈都会出现。但是,我设置了身份验证,因此当我执行以下操作(如下)时,我只看到地图而不是圆圈:

ContentView.swift

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var auth: AuthenticationState

    var body: some View {
        ZStack {
            if auth.user != nil {
                HomeView()
            } else {
                GatekeeperView()
            }
        }
    }
}

HomeView.swift

struct HomeView: View {
    var body: some View {
        ZStack {
            Circle()
                .fill(Color.blue)
                .opacity(1)
                .frame(width: 32, height: 32)
            
            MapView().edgesIgnoringSafeArea(.all)
        }
    }
}

经过身份验证的用户可以看到 HomeView。不幸的是,圆圈没有显示出来。

【问题讨论】:

  • 尝试在ZStack中为您的视图显式设置.zIndex()
  • 圆圈在地图视图下方
  • 非常感谢!有效:D

标签: ios swift swiftui


【解决方案1】:

尝试在ZStack 中为您的视图显式设置.zIndex()

struct HomeView: View {
    var body: some View {
        ZStack {
            Circle()
                .fill(Color.blue)
                .opacity(1)
                .frame(width: 32, height: 32)
                .zIndex(1)

            MapView().edgesIgnoringSafeArea(.all)
                .zIndex(2)
        }
    }
}

或者重新排序您的ZStack

struct HomeView: View {
    var body: some View {
        ZStack {
            MapView().edgesIgnoringSafeArea(.all)

            Circle()
                .fill(Color.blue)
                .opacity(1)
                .frame(width: 32, height: 32)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 2011-04-13
    • 2020-02-06
    • 2019-10-08
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多