【问题标题】:full screen background image with vstack带有 vstack 的全屏背景图像
【发布时间】:2020-03-25 16:11:55
【问题描述】:

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

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

我期待里面的文字...

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")
            }
        }
    }
}

【问题讨论】:

    标签: swiftui vstack


    【解决方案1】:

    这是一个稍微修改的变体。使用 Xcode 11.4(最终发布)/iOS 13.4 测试

    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)
        }
    }
    

    【讨论】:

    【解决方案2】:

    如果需要使用NavigationView,并保持图片的纵横比,可以这样做:

    import SwiftUI
    
    struct FullScreenPictureDemo: View {
        var body: some View {
            NavigationView {
                ZStack {
                    Image("your_full_screen_background_picture")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .edgesIgnoringSafeArea(.all)
                        .scaledToFill()
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2023-03-16
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多