我正在尝试使图像填满屏幕(包括 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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码产生以下结果,顶部和底部仍然有白条。有没有办法让图像真正填满屏幕?
默认情况下,SwiftUI 会考虑安全区域。为了获得所需的结果,您必须通知 SwiftUI 它可以忽略安全区域,并将以下语句附加到最后一个对象:
.edgesIgnoringSafeArea(.all)
Run Code Online (Sandbox Code Playgroud)
这意味着新代码将是:
ZStack() {
Image("background")
.resizable()
.aspectRatio(UIImage(named: "background")!.size, contentMode: .fill)
Text("SwiftUI Example")
.font(.largeTitle)
.background(Color.black)
.foregroundColor(.white)
}.edgesIgnoringSafeArea(.all)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1828 次 |
| 最近记录: |