安全区域上方的 SwiftUI 全屏图像
SwiftUI full-screen image above safe areas
我正在尝试让图像填满屏幕(包括 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)
我正在尝试让图像填满屏幕(包括 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)