首页 > 解决方案 > space around top of the background image

问题描述

Trying to set an image as a background for my view in SwiftUI. Using Xcode 12 beta and 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()
    }
}

}

Output

enter image description here

标签: swiftuiios14

解决方案


The following should be enough (tested with Xcode 11.4 / iOS 13.4)

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

推荐阅读