Reputation: 683
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.
Upvotes: 1
Views: 2333
Reputation: 4178
Follow below constraint and you will see the magic of Auto layouts
ImageView constraints:
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:
Label TrailingSpace constraint should not be a constant value, instead set it to standard spacing (i.e 8 pts).
Second label constraints:
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
Update:
ImageView vertical constraint to cell:
Label1 TrailingSpace to cell with standard spacing:
Label2 TrailingSpace to cell with standard spacing and BottomSpace to cell with standard spacing.
Upvotes: 4
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
Reputation: 8324
Try cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
Upvotes: 3
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
Reputation: 2397
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