5

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

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

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

如果我把模拟器中的 iPhone 转到一边,我有左右白色条纹。
我怎样才能改变这个?

4

2 回答 2

21

这是使用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

结果

肖像 景观

于 2019-08-25T20:31:50.040 回答
0

这应该在没有 GeometryReader 的情况下工作:

var body: some View {
       ZStack {
            ZStack {}
            .frame( maxWidth: .infinity, maxHeight: .infinity)
            .background(Image("Background LaunchScreen").resizable())
            .ignoresSafeArea()
            VStack {
                Text("Text")
                Text("Text")
                Text("Text")
                Text("Text")
                Spacer()
            }
        }
}

于 2021-12-19T23:37:44.250 回答