安全区域上方的SwiftUI全屏图像

安全区域上方的SwiftUI全屏图像,swiftui,Swiftui,我正试图让一幅图像填满整个屏幕,包括iPhone X系列凹口下方和应用程序切换栏附近的安全区域 import SwiftUI struct ContentView : View { var body: some View { ZStack() { Image("background") .resizable() .aspectRatio(UIImage(named: "backgro

我正试图让一幅图像填满整个屏幕,包括iPhone X系列凹口下方和应用程序切换栏附近的安全区域

import SwiftUI

struct ContentView : View {
    var body: some View {
        ZStack() {
            Image("background")
                .resizable()
                .aspectRatio(UIImage(named: "background")!.size, contentMode: .fill)
            Text("SwiftUI Example")
                .font(.largeTitle)
                .background(Color.black)
                .foregroundColor(.white)
        }
    }
}
上面的代码生成以下结果,在顶部和底部仍然有白色条。有没有办法让图像真正充满屏幕


默认情况下,SwiftUI会考虑安全区域。为了获得所需的结果,您必须通知SwiftUI它可以忽略安全区域,并在最后一个对象后附加以下语句:

.edgesIgnoringSafeArea(.all)
这意味着新代码将是:

ZStack() {
    Image("background")
        .resizable()
        .aspectRatio(UIImage(named: "background")!.size, contentMode: .fill)
    Text("SwiftUI Example")
        .font(.largeTitle)
        .background(Color.black)
        .foregroundColor(.white)
}.edgesIgnoringSafeArea(.all)

默认情况下,SwiftUI会考虑安全区域。为了获得所需的结果,您必须通知SwiftUI它可以忽略安全区域,并在最后一个对象后附加以下语句:

.edgesIgnoringSafeArea(.all)
这意味着新代码将是:

ZStack() {
    Image("background")
        .resizable()
        .aspectRatio(UIImage(named: "background")!.size, contentMode: .fill)
    Text("SwiftUI Example")
        .font(.largeTitle)
        .background(Color.black)
        .foregroundColor(.white)
}.edgesIgnoringSafeArea(.all)