【问题标题】:space around top of the background image背景图像顶部周围的空间
【发布时间】:2020-07-17 06:37:54
【问题描述】:

尝试将图像设置为我在 SwiftUI 中的视图的背景。使用 Xcode 12 测试版和 iOS 14。

struct ContentView: View {
var body: some View {
    ZStack {
        Image("background")
            .resizable()
            .scaledToFill()
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height, alignment: .center)
            .clipped()
    }
}

}

输出

【问题讨论】:

    标签: swiftui ios14


    【解决方案1】:

    以下应该足够了(使用 Xcode 11.4 / iOS 13.4 测试)

    var body: some View {
        ZStack {
            Image("background")
                .resizable()
                .scaledToFill()
                .edgesIgnoringSafeArea(.all)
        }
    }
    

    【讨论】:

    • 我试过了,但我想知道为什么我需要添加它?
    • @karmjitsingh,让它在安全区域之外全屏显示。
    • 如果我删除修饰符,为什么它只在顶部显示空间,为什么不在底部显示。
    • @karmjitsingh,您将帧硬编码为屏幕大小,但安全区域的中心不在您调整大小的物理屏幕的中心,因此您可以看到自己得到了什么。
    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多