简体   繁体   中英

How do I set the background image size of a table cell in swift?

So far I have:

var imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 10, cell.frame.height - 10))
let image = UIImage(named: ImageNames[indexPath.row])
imageView.image = image
cell.backgroundView = imageView

This sets the background image, but my goal is to have 10 pixels worth of padding on either side. This is just filling the background of the cell with this image.

I actually was able to fix my own problem. I just created the background view and then added a subview to it like this:

let imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 10, cell.frame.height - 10))
let image = UIImage(named: "Image Name")
imageView.image = image
cell.backgroundView = UIView()
cell.backgroundView!.addSubview(imageView)

This prevents anything from being covered up and achieves the effect I wanted.

Use

var imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 10, cell.frame.height - 10))
let image = UIImage(named: ImageNames[indexPath.row])
imageView.image = image
//Just add imageView as subview of cell
cell.addSubView(imageView)
cell.sendSubviewToBack(imageView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM