I'm trying to give my ListView a background image which should not be for every cell. So only one image for the whole view.
I have tried to add .background(Image("nameOfImage))
as list modifier but did not do anything.
NavigationView {
List(elements) { element in
NavigationLink(destination: NextView) {
Text(element.name)
Image(element.image)
}
}.background(Image("nameOfImage"))
}
I have also tried a ZStack but I think the list overlays my image
So how can I put an image as list background or view background (the code above is the whole view)
It did the thing, but the point is that UITableView
and UITableViewCell
have default white backgroundColor
. You should make them transparent to see through.
Something like:
struct ContentView: View {
init() {
UITableView.appearance().backgroundColor = .clear // For tableView
UITableViewCell.appearance().backgroundColor = .clear // For tableViewCell
}
var body: some View {
NavigationView {
List(0...10, id: \.self) { number in
Text("\(number)")
}.background(Image("BG"))
}
}
}
Note that I have simplified my answer to make it independent from other parts of your project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With