【问题标题】:How to push a view to the top of a ZStack?如何将视图推送到 ZStack 的顶部?
【发布时间】:2021-07-24 02:46:22
【问题描述】:

所以,基本上我创建了一个 HeaderView,它由一个占用整个屏幕空间的 ZStack 组成,因为它添加了纯黑色背景和位于黑色背景上的整个标题(因此需要 Zstack)。一切看起来都是我想要的,但是我很难将我的 HeaderView 放在 ZStack 的顶部,以便为我需要在标题下方添加的所有其他视图腾出空间。

我当前的代码是:


struct HomeView: View {
    
    var body: some View {
            ZStack() {
                Color.black.edgesIgnoringSafeArea(.all)
                HeaderView()
                
            }
    }
}



struct HeaderView: View {
    let name = "John"
    let lastName = "Gonzalez"
    let points = Int.random(in: 4000..<6000)
    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 5) {
                Text(self.name)
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                Text(self.lastName)
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                
                HStack {
                    Text("Premium")
                        .frame(width: 90, height: 30)
                        .background(Color.green)
                        .cornerRadius(7)
                        .foregroundColor(.white)
                    
                    
                    Image(systemName: "bolt.fill")
                        .foregroundColor(.yellow)
                        .padding(.leading, 10)
                    Text(String(self.points))
                        .foregroundColor(.white)
                    Text("Points")
                        .foregroundColor(.white)
                        .font(.callout)
                        .fontWeight(.bold)
                }
                .padding(.top, 13)
            }
            .padding(.horizontal, 20)
            Spacer()
            
            Image("profilePicture")
                
                .resizable()
                .scaledToFit()
                .frame(width: 100, height:100)
                .clipShape(Capsule())
                .shadow(radius: 10)
                .padding(.trailing, 1)
        }
        
    }
    
}

视图当前看起来像这样,虽然我真的希望它像每个标题一样位于顶部。

有什么建议吗?例如,我尝试在调用 HeaderView() 之后添加一个 Spacer(),以便将 Header 推到 ZStack 的顶部,但这实际上什么也没做。任何帮助表示赞赏!

【问题讨论】:

标签: swiftui zstack


【解决方案1】:

您可以使用VStack 并使用.backgroundColor

struct HomeView: View {
    var body: some View {
        VStack() {
            HeaderView()
            Spacer()
        }
        .background(Color.black)
        .edgesIgnoringSafeArea(.all)
    }
}

【讨论】:

  • 谢谢!这似乎是我需要的。无论出于何种原因,我似乎总是与 ZStacks 斗争。
  • @Alejo 如果答案有效,请接受和/或投票:)
【解决方案2】:

您可以将.top 与您的ZStack 对齐:

struct HomeView: View {
    var body: some View {
        ZStack(alignment: .top) {
            YourContent()
            HeaderView()
        }
        .background(Color.black)
        .edgesIgnoringSafeArea(.all)
    }
}

【讨论】:

  • 是的,我确实尝试过。但是,这将使我在 ZStack 中的所有视图都与顶部对齐,我不想这样做,我只想在顶部是我的标题。我在这里错过了什么吗?
  • 在这种情况下,您有多种选择: A) 在您的内容中添加“.padding(.top, X)”; B)使用'VStack(spacing:0)'而不是'ZStack'或C)通过.overlay(Header(),对齐:.top)添加标题,这样你将只有顶部的标题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
相关资源
最近更新 更多