代码之家  ›  专栏  ›  技术社区  ›  Rashik

为什么Spacer()会自动扩展我的VStack

  •  1
  • Rashik  · 技术社区  · 9 月前

    This is an image of my xcode along with the code and the canvas of whats happening.

    我使用Color.green.ignoreSafeArea()为ZStack的背景着色。 但是,当我将第34行的背景设置为背景(Color.white)时,它也会将安全区域之外的区域设置为白色。我不知道为什么,它不应该只为VStack的背景着色吗?

    This is an Image of UI when I use the below code

    当我在第34行中使用背景(Color.white.cornerRadius(10))时,它开始正常运行,只覆盖安全区域? 有人对此有任何解释吗,还是只是一个bug?

    以下是图像中使用的代码:

    struct MySignUpView: View {
    var body: some View {
        VStack(spacing: 20){
            Image(systemName: "chevron.up")
                .padding(.top)
            Text("Sign up")
                .font(.headline)
                .fontWeight(.semibold)
            Image(systemName: "flame.fill")
                .resizable()
                .scaledToFit()
                .frame(width: 100, height: 100)
            Text("This is the best app in the world, if you subscribe to this application you will become a billionaire by age 30 .")
                .multilineTextAlignment(.center)
            
            Text("CREATE AN ACCOUNT")
                .font(.headline)
                .padding()
                .padding(.horizontal)
                .foregroundStyle(Color.white)
                .background(Color.black.cornerRadius(10))
            Spacer()
        }
        .frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
        .background(Color.white.cornerRadius(20))
    }
    

    }

    1 回复  |  直到 9 月前
        1
  •  0
  •   Sweeper    9 月前

    background(Color.white) 忽略安全区域,因为 background 修饰符也接受a ignoresSafeAreaEdges 参数,其默认值为 .all .

    请注意,上述 背景 方法需要a ShapeStyle ,其中 Color.white 符合。但是,当你这样做的时候 .background(Color.white.cornerRadius(10)) , Color.white.cornerRadius(10) 形状样式 .这叫 另一个 ,已弃用超载 背景 这需要任何 View - background(_:alignment:) 。默认情况下,此修改器不会忽略安全区域。

    其非弃用版本为 background(alignment:content:) .

    另请参见 my answer here ,这是一种非常相似的情况。