add a background image to a tableview

I want to add a background image to a table view using Swift. IB allows background colors but not images. Is there a way to do this in Swift?

The result I want is for the cell to have a transparent background so the background image shows through. Is this possible?

Answered by junkpile in 41492022

Yes, UITableView has a backgroundView property which you can set to a UIImageView you create. You would most likely want to do this in your view controller's viewDidLoad method.

Accepted Answer

Yes, UITableView has a backgroundView property which you can set to a UIImageView you create. You would most likely want to do this in your view controller's viewDidLoad method.

Hello,


This is my solution:

1) Setbackground for UIView.

2) Setbackground for TableView, TableViewCell is ClearColor.


PS: extension function



import UIKit
extension UIView {
    func addBackground(imageName:String) {
        /
        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height
       
        let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
        imageViewBackground.image = UIImage(named: imageName)
       
        /
        imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill
       
        self.addSubview(imageViewBackground)
        self.sendSubviewToBack(imageViewBackground)
    }
}
add a background image to a tableview
 
 
Q