SwiftUI 全屏图像背景

Cod*_*ott 2 background image swiftui

我想在背景中有一个全屏图像。我已经实现了这个:

struct LoginView: View {
    var body: some View {
        VStack {
            Spacer();
            Text("Hallo");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Text("Hallo2");
            Spacer();
            }.background(Image("Background LaunchScreen")
                .resizable()
                .aspectRatio(UIImage(named: "Background LaunchScreen")!.size, contentMode: .fill)
                .clipped())
            .edgesIgnoringSafeArea(.all)
    }
}
Run Code Online (Sandbox Code Playgroud)

当我移除垫片时,图像不再以全屏模式显示。
当然可以用不同的方式解决吗?

如果我将模拟器中的 iPhone 翻到一边,就会出现左右白色条纹。
我怎样才能改变这个?

bac*_*h-f 19

这是使用GeometryReaderand的可能解决方案ZStack

import SwiftUI

struct LoginView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                Image("LaunchImage")
                    .resizable()
                    .aspectRatio(geometry.size, contentMode: .fill)
                    .edgesIgnoringSafeArea(.all)
                VStack {
                    ForEach (1...10, id: \.self) { _ in
                        Text("Hallo")
                            .foregroundColor(Color.white)
                    }
                }
            }
        }
    }
}

#if DEBUG
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

结果

肖像 风景

  • 这使得图像增加 ZStack,如果我将某些东西向左对齐,它会从屏幕外开始 (2认同)