【发布时间】:2020-09-07 12:12:51
【问题描述】:
我正在尝试使用 SwiftUI 为我的屏幕设置背景图像。 我注意到屏幕顶部大约 10 像素处有某种插图,我不确定那是什么?以及如何从最顶部开始图像。
这是我的代码:
struct AppBackGround: ViewModifier {
func body(content: Content) -> some View {
ZStack {
Image("background")
.resizable()
.edgesIgnoringSafeArea(.all)
.scaledToFill()
.frame(width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height, alignment: .top)
// .clipped()
.border(Color.red, width: 1)
.Print(UIScreen.main.bounds.height)
content
}
.background(Color.red)
}
}
extension View {
func withAppBackground() -> some View {
self.modifier(AppBackGround())
}
}
struct AppBackGround_Previews: PreviewProvider {
static var previews: some View {
Group {
ZStack {
Text("iphone 8 plus")
}
.withAppBackground()
.colorScheme(.light).previewDevice("iPhone 8 Plus")
ZStack {
Text("iphone 11")
}
.withAppBackground()
.colorScheme(.light).previewDevice("iPhone 11")
}
}
}
在下图中,您可以看到有一个红色边框围绕着图像的确切框架。您会注意到图像顶部和屏幕顶部之间有一个空间。
这也是原图,如果你想试试看。
【问题讨论】: