Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SwfitUI set background image for list not cell

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)

like image 269
Uni_x Avatar asked Oct 14 '25 04:10

Uni_x


1 Answers

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

like image 154
Mojtaba Hosseini Avatar answered Oct 17 '25 21:10

Mojtaba Hosseini