简体   繁体   中英

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. 在此处输入图片说明

在此处输入图片说明

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 (ie 8 pts).

Second label constraints:

在此处输入图片说明

Labels TrailingSpace and BottomSpace constraint should not be a constant value, instead set it to standard spacing (ie 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.

在此处输入图片说明

尝试cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill

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

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

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.

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