简体   繁体   English

为什么 ZStack 在 SwiftUI 中不能使用设备高度?

[英]How come ZStack is not working with device height in SwiftUI?

So I was working on a project and whenever I make a ZStack to get a background color, the subsequent VStack is basically somehow attached to the ZStack.所以我在做一个项目,每当我制作一个 ZStack 来获得背景颜色时,随后的 VStack 基本上都会以某种方式附加到 ZStack。 I thought ZStack were similar to objects being stacked on top of each other.我认为 ZStack 类似于相互堆叠的对象。

However, the closer I make the bottomHeightMultiplier var closer to 100% the more the Text in the Vstack gets pushed off the screen.但是,我使 bottomHeightMultiplier var 越接近 100%,Vstack 中的文本就越多地被推离屏幕。 I was just trying to create a background view with the top 20% of any device screen being white and the bottom 80% of the screen being green.我只是想创建一个背景视图,其中任何设备屏幕的前 20% 为白色,屏幕底部 80% 为绿色。

Unfortunately the Text("Enter bill total") just ends up getting pushed off the screen.不幸的是,Text("Enter bill total") 最终被推离了屏幕。 If I put the Spacer() below the Text, it gets pushed to the top and beyond the safe area.如果我将 Spacer() 放在文本下方,它会被推到顶部并超出安全区域。 Putting it above the Text pushes it to bottom and beyond the safe area.将它放在文本上方会将其推到底部并超出安全区域。



import SwiftUI

struct CalculatorScreen: View {
    
    var screenWidth = UIScreen.main.bounds.width
    var screenHeight = UIScreen.main.bounds.height
    var topHeightMultiplier: CGFloat = 0.20
    var bottomHeightMultiplier: CGFloat = 0.80

    
    var body: some View {
        
        ZStack {
 

            VStack {
                Color.white
                    .frame(minHeight: screenHeight*topHeightMultiplier)
                
                Color.green
                    .frame(minHeight: screenHeight*bottomHeightMultiplier)
            }


            VStack {
                Spacer()
                Text("Enter bill total")
                    .foregroundColor(.black)

            }   
            
        }
        
    }
}



This will do the trick.这样就可以了。 What really is happening is that when using UIScreen.bounds , the safe areas create a problem, making the view longer than the screen long is.真正发生的是,当使用UIScreen.bounds时,安全区域会产生问题,使视图比屏幕长。 That means that the inner VStack pushes on the outer ZStack and thus makes the ZStack also longer than the device is.这意味着内部 VStack 推动外部 ZStack,从而使 ZStack 也比设备更长。 Thus, no the objects within the ZStack are completely independent, however the size of the parent changes when that of a child changes, implicitly affecting other child objects in the ZStack.因此,ZStack 中的对象不是完全独立的,但是当子对象的大小发生变化时,父对象的大小也会发生变化,从而隐式地影响 ZStack 中的其他子对象。 The second VStack will be as long as the ZStack due to the Spacer(), making it look like it has been pushed off the screen.由于 Spacer(),第二个 VStack 将与 ZStack 一样长,使其看起来像是被推离了屏幕。

Additionally, the VStack for the colors had a standard padding (now added spacing: 0 ), creating a white line between the colors, eating up space as well which is not taken into account when calculating the height of the colors. My tip is to always use colors you can see when building blocks, that way you immediately spot safe area issues and unwanted paddings.此外,colors 的 VStack 有一个标准填充(现在添加了spacing: 0 ),在 colors 之间创建了一条白线,在计算 colors 的高度时也没有考虑到这一点。我的建议是始终使用 colors 你可以在构建块时看到,这样你可以立即发现安全区域问题和不需要的填充。

struct CalculatorScreen: View {
    
    var topHeightMultiplier: CGFloat = 0.20
    var bottomHeightMultiplier: CGFloat = 0.80

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                VStack(spacing: 0) {
                    Color.purple
                        .frame(minHeight: geometry.size.height*topHeightMultiplier)
                    Color.green
                        .frame(minHeight: geometry.size.height*bottomHeightMultiplier)
                }
                .ignoresSafeArea()

                VStack {
                    Spacer()
                    Text("Enter bill total")
                        .foregroundColor(.black)

                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM