SwiftUI has an interesting way to change the background of the whole view, by creating a new view which will be the background.
The easiest way to change background is to create a view being background, for example, Color or Image view.
You need to embed your main content and background with ZStack.
If you want to set some color as background use Color.
var body: some View {
ZStack {
Color.blue
.edgesIgnoringSafeArea(.all)
Text(“Hello :)”)
.font(.largeTitle)
}
}
Result:
To set some kind of image as a background, replace Color.blue with Image view.
var body: some View {
ZStack {
Image(“background”)
.edgesIgnoringSafeArea(.all)
Text(“Hello :)”)
.font(.largeTitle)
}
}
Result:
Remember you need to have some image with the name “background” in Assets.xcassets.
There is used .edgesIgnoringSafeArea which ignores safe area bounds.