Vadim F.
Vadim F.

Reputation: 1051

Constraints don't work when setting an UIImage inside a TableViewCell

When I am setting the UIImage in cellForRowAt function it looks like the UIImage I am setting overlaps the UIImageView.

EDITED : clipsToBound is marked and contentMode is set to .scaleAspectFit in the Storyboard

I am setting all the Constraints in the Storyboard as usual:

enter image description here

But when I run the app it looks something with a bigger priority overpowering the Constraints I set and this is what I get :

enter image description here

A while ago I did a similar thing in other project which worked perfectly fine for me :

enter image description here

Here is the code for setting the cells, though I am pretty sure I just accidentally marked or unmarked something ing the Storyboard:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return songArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
    cell.showLoader()
    cell.songNameLabel.text = songArray[indexPath.row].title

    if songImageArray[songArray[indexPath.row].title] != nil {
        cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
        cell.hideLoader()
    }
    cell.setupImageDesign()
    return cell
}

Please let me know if any additional information needed, Thank you in advance.

Upvotes: 0

Views: 562

Answers (5)

vishnu anilkumar
vishnu anilkumar

Reputation: 230

Change the contentMode of UIImageView in storyboard and also set clipsToBounds = true.

Upvotes: 0

Nikunj Kumbhani
Nikunj Kumbhani

Reputation: 3924

You have an issue with Constrain to margins

Please remove all the constraints and make sure unchecked this option before setting the constraints.

enter image description here

In Storyboard looks like this

enter image description here

Upvotes: 0

Yervand Saribekyan
Yervand Saribekyan

Reputation: 502

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
    cell.showLoader()
    cell.songNameLabel.text = songArray[indexPath.row].title

    if songImageArray[songArray[indexPath.row].title] != nil {
        cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
        cell.hideLoader()
    }
    cell.setupImageDesign()
    return cell
}

cell.imageView?.image is default imageView from UITableViewCell.
You must rename your custom UIImageView outlet

Upvotes: 1

Ajay saini
Ajay saini

Reputation: 2470

Change ContentMode of ImageView to .scalToFill and clipping property to clipToBounds

Upvotes: 0

Faipdeoiad
Faipdeoiad

Reputation: 122

Set the image view clipsToBoundsto true.

Upvotes: 1

Related Questions