Search code examples
swiftimageimageviewtableviewcell

How add Size and Position to ImageView in UITableView Cell?


I added a image to my UITableView Cell:

cell.imageView!.image = UIImage(named: "Default.png")

Everything works, the image is displayed. Now I want to give a custom size and position for the image, so I try it with this code:

cell.imageView!.frame = CGRect(x: 29, y: 17, width: 14, height: 13)

But for any reason it doesn't work. Any advices?


Solution

  • If your position will not change You can create frame in your TableViewCell

    class TableViewCell: UITableViewCell {
    
    @IBOutlet weak var imageView: UIImageView!
    
    override func layoutSubviews() {
        super.layoutSubviews()
    
        self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
    }
    }
    

    You can change frame in tableview(cellForRowAt) but If frame will not change its a bad because cellForRowAt run when scrolling up - down or loading cell . You can define in UITableViewCell once.