vstack全屏背景图像

时间:2020-03-25 16:11:55

标签: swiftui vstack

我希望有一个带有导航视图的全屏背景图像(必须在最上面,因为它是从基本视图而不是通常在“此”视图中)。 在此视图中,我想要一个VStack,它位于安全区域之内,因此位于导航栏和底部布局之间。

不幸的是我得到了(见图)

我希望里面的文本... enter image description here

struct ContentView: View {
    var body: some View {
        NavigationView {
            ZStack(alignment: .center) {

                Image("laguna")
                    .resizable()
                    .edgesIgnoringSafeArea(.all)
                    .scaledToFill()
                    .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

                VStack(alignment: .center) {
                    Text("just a test")
                        .font(.largeTitle)
                        .foregroundColor(Color.white)
                    Spacer()
                    Text ("not centered....why?")
                        .font(.largeTitle)
                        .foregroundColor(Color.white)

                }
                .zIndex(4)
                .navigationBarTitle("nav bar title")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个经过修改的变体。经过Xcode 11.4(最终发布)/ iOS 13.4的测试

demo

struct TestFullScreenImage: View {
    var body: some View {
        NavigationView {
            ZStack {
                Image("large_image")
                    .resizable()
                    .edgesIgnoringSafeArea(.all)
                    .scaledToFill()

                VStack {
                    Text("Just a test")
                        .font(.largeTitle)
                        .foregroundColor(.white)
                        Spacer()
                    Text("centered")
                        .font(.largeTitle)
                        .background(Color.green)
                }
                .navigationBarTitle("Navigation Title")
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}