libra
libra

Reputation: 683

Image not fit to imageview in custom tableview cell

I'm trying to load a image to my tableview cell, however, the image seems to be bigger than the imageview and exceed the boundary of imageview. I tried to set the contentMode property of the UIImageView to ScaleAspectFit but it didn't solve my problem.

Code :

cell.imageView?.contentMode = UIViewContentMode.Center
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
switch (post.priorityLevel){
    case "High Priority Posting":
        cell.imageView?.image = UIImage(named: "High Priority Posting")
    case "Priority Posting":
        cell.imageView?.image = UIImage(named: "Priority Posting")
    case "General Posting":
        cell.imageView?.image = UIImage(named: "General Posting")
    default:
        print("Priority Level can't be loaded to table")
}

I added screenshots of my table view from Interface builder and the result on simulator.

Any suggestions will be appreciated. enter image description here

enter image description here

Upvotes: 1

Views: 2333

Answers (5)

Bharat Modi
Bharat Modi

Reputation: 4178

Follow below constraint and you will see the magic of Auto layouts

ImageView constraints:

enter image description here

Set the fix height and width you want to display for the imageView, and set VerticalCentre constraint. Set imageView's content mode to Aspect Fit.

Top label constraints:

enter image description here

Label TrailingSpace constraint should not be a constant value, instead set it to standard spacing (i.e 8 pts).

Second label constraints:

enter image description here

Labels TrailingSpace and BottomSpace constraint should not be a constant value, instead set it to standard spacing (i.e 8 pts), and set its VerticalContentHugging priority to 250

enter image description here

Update:

ImageView vertical constraint to cell:

enter image description here

Label1 TrailingSpace to cell with standard spacing:

enter image description here

Label2 TrailingSpace to cell with standard spacing and BottomSpace to cell with standard spacing.

enter image description here

Upvotes: 4

Sanjan Piya
Sanjan Piya

Reputation: 247

If its a custom table view cell, dont name the imageView outlet to "imageView", name it to any other for eg: "cellImageView". and then try setting clipsToBound to true of your cell cellImageView.

Upvotes: 0

Nimisha Ranipa
Nimisha Ranipa

Reputation: 8324

Try cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill

Upvotes: 3

Subin K Kuriakose
Subin K Kuriakose

Reputation: 849

UIImage

1) first you set the imageview leading space ,equal height,center vertically to container 2)set height itself

City and State

1) set horizontal space and Top to UiimageView, Trailing space to container view 2)set height itself

Description label

1)set horizontal space and Bottom to UiimageView, Trailing space to container view

2)set height itself story board -> set lines = 0

Upvotes: 0

jood
jood

Reputation: 2397

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson7.html#//apple_ref/doc/uid/TP40015214-CH8-SW1

In this official tutorial, they design a tableview exactly like yours. I think your main issue has to do with the lack of constraints/autolayout

Upvotes: 0

Related Questions